【Word Search】cpp
题目:
Given a 2D board and a word, find if the word exists in the grid.
The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
For example,
Given board =
[
["ABCE"],
["SFCS"],
["ADEE"]
]
word = "ABCCED", -> returns true,
word = "SEE", -> returns true,
word = "ABCB", -> returns false.
代码:
class Solution {
public:
bool exist(vector<vector<char> >& board, string word)
{
vector<vector<bool> > visited;
for ( size_t i = ; i < board.size(); ++i )
{
vector<bool> tmp(board[i].size(),false);
visited.push_back(tmp);
}
for ( int i = ; i < board.size(); ++i )
{
for ( int j = ; j < board[i].size(); ++j )
{
if (Solution::dfs(board, visited, i, j, word, )) return true;
}
}
return Solution::dfs(board, visited, , , word, );
}
static bool dfs(
vector<vector<char> >& board,
vector<vector<bool> >& visited,
int i,
int j,
string& word,
int curr )
{
if ( curr==word.size() ) return true;
if ( i< || i==board.size() || j< || j==board[i].size() ) return false;
if ( visited[i][j] ) return false;
if ( board[i][j]!=word[curr] ) return false;
if ( board[i][j]==word[curr] )
{
visited[i][j] = true;
if ( Solution::dfs(board, visited, i, j+, word, curr+)) return true;
if ( Solution::dfs(board, visited, i+, j, word, curr+)) return true;
if ( Solution::dfs(board, visited, i, j-, word, curr+)) return true;
if ( Solution::dfs(board, visited, i-, j, word, curr+)) return true;
}
visited[i][j] = false;
return false;
}
};
tips:
好好领悟一下dfs吧。。。
1. 这道题在主程序中有一个循环,如果一旦发现word的起点,就从这个位置开始dfs,看能否返回结果。
2. dfs的过程跟模板一样。
shit
============================================
第二次过这道题,逻辑清晰了很多,修正了两个笔误bug,AC了。
class Solution {
public:
bool exist(vector<vector<char> >& board, string word)
{
for ( int i=; i<board.size(); ++i )
{
for ( int j=; j<board[i].size(); ++j )
{
if ( board[i][j]==word[] )
{
board[i][j] = '#';
if ( Solution::dfs(board, i, j, word.substr(,word.size()-)) ) return true;
board[i][j] = word[];
}
}
}
return false;
}
static bool dfs(
vector<vector<char> >& board,
int i, int j,
string word)
{
if ( word.size()== ) return true;
// left
if ( j->= && board[i][j-]==word[] )
{
board[i][j-] = '#';
if (Solution::dfs(board, i, j-, word.substr(,word.size()-)) ) return true;
board[i][j-] = word[];
}
// right
if ( j+<board[].size() && board[i][j+]==word[] )
{
board[i][j+] = '#';
if (Solution::dfs(board, i, j+, word.substr(,word.size()-)) ) return true;
board[i][j+] = word[];
}
// up
if ( i->= && board[i-][j]==word[] )
{
board[i-][j] = '#';
if (Solution::dfs(board, i-, j, word.substr(,word.size()-)) ) return true;
board[i-][j] = word[];
}
// down
if ( i+<board.size() && board[i+][j]==word[] )
{
board[i+][j] = '#';
if (Solution::dfs(board, i+, j, word.substr(,word.size()-)) ) return true;
board[i+][j] = word[];
}
return false;
}
};
第二次的代码,比第一次过的代码还优化了额外空间结构。O(1)额外空间。
【Word Search】cpp的更多相关文章
- 【word ladder】cpp
题目: Given two words (beginWord and endWord), and a dictionary, find the length of shortest transform ...
- 【Word Break】cpp
题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-se ...
- 【word xml】将word转化为xml格式后,如何在xml中卫word添加分页符
1.首先在xml中找到我们需要添加分页符的位置 例如:我需要在这个第一部分上面添加一个分页符 2.找到这个[第一部分]这个位置之后,开始往上找,找到对应的位置 3.在</w:pPr>下方添 ...
- hdu 4739【位运算】.cpp
题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...
- Hdu 4734 【数位DP】.cpp
题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...
- 【Text Justification】cpp
题目: Given an array of words and a length L, format the text such that each line has exactly L charac ...
- 【Merge Intervals】cpp
题目: Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6], ...
- 【Insert Interval】cpp
题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nec ...
- 【Edit Distance】cpp
题目: Given two words word1 and word2, find the minimum number of steps required to convert word1 to w ...
随机推荐
- 关于node中的板块问题
最近自己在看node实战那本书,不过发现有一些书上不对的地方,罗列如下:1.connect服务自己有一些中间件可供使用,但是书上说的有cookie-parser.logger.favicon和body ...
- Apache Spark 2.2.0 中文文档 - GraphX Programming Guide | ApacheCN
GraphX Programming Guide 概述 入门 属性 Graph 示例属性 Graph Graph 运算符 运算符的汇总表 Property 运算符 Structural 运算符 Joi ...
- 利用python进行简单的图片处理
python的 PIL模块是专门用来处理图片的模块,其功能可以说是非常强大了. 演示环境:win7 操作系统 安装python2.7以及指定的对应PIL模块 我这里有一个现成的PIL模块.本文的大部分 ...
- 转:ZedGraph 各属性含义(中文)
简介:ZedGraph 是一个开源的.NET图表类库, 全部代码都是用C#开发的.它可以利用任意的数据集合创建2D的线性和柱形图表. 属性名称 属性值.作用 MasterPane 一个类对象管理多个G ...
- 几幅手稿讲解CNN
学习深度神经网络方面的算法已经有一段时间了,对目前比较经典的模型也有了一些了解.这种曾经一度低迷的方法现在已经吸引了很多领域的目光,在几年前仅仅存在于研究者想象中的应用,近几年也相继被深度学习方法实现 ...
- centos6.2安装内核
http://vault.centos.org/6.2/updates/Source/SPackages/ yum install rpm-build redhat-rpm-config unifde ...
- python 学习之FAQ:find 与 find_all 使用
FAQ记录 1. 错误源码 错误源码如下 def fillUnivList(_html,_ulist): soup =BeautifulSoup(_html,'html.parser') fo ...
- 基于mllib的协同过滤实战(电影推荐)
//加载需要的包 import org.apache.spark.rdd._ import org.apache.spark.mllib.recommendation.{ALS, Rating, Ma ...
- C#环形缓冲区(队列)完全实现
公司项目中经常设计到串口通信,TCP通信,而且大多都是实时的大数据的传输,然后大家都知道协议通讯肯定涉及到什么,封包.拆包.粘包.校验--什么鬼的概念一大堆,说简单点儿就是要一个高效率可复用的缓存区. ...
- 【HHHOJ】NOIP2018 模拟赛(二十五) 解题报告
点此进入比赛 得分: \(100+100+20=220\)(\(T1\)打了两个小时,以至于\(T3\)没时间打了,无奈交暴力) 排名: \(Rank\ 8\) \(Rating\):\(+19\) ...