51. Word Search
Word Search
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.
思路:深度优先搜索。注意每条路径搜索完之后,所有的 visited 重置为 false (未访问).
typedef pair<int, int> point;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
bool dfs(vector<vector<char> > &board, string& word, int id, point p, vector<vector<bool> > &visited) {
if(id == word.size()) return true;
visited[p.first][p.second] = true;
for(int i = 0; i < 4; ++i) {
int x = p.first + dx[i], y = p.second +dy[i];
if(x < 0 || x >= board.size() || y < 0 || y >= board[0].size() || visited[x][y]) continue;
if(board[x][y] == word[id] && dfs(board, word, id+1, point(x, y), visited))
return true;
visited[x][y] = false;
}
return false;
}
class Solution {
public:
bool exist(vector<vector<char> > &board, string word) {
if(word == "") return true;
if(board.size() == 0 || board[0].size() == 0) return false;
int row = board.size(), col = board[0].size();
vector<vector<bool> > visited(row, vector<bool>(col, 0));
for(int r = 0; r < row; ++r) {
for(int c = 0; c < col; ++c) {
if(board[r][c] == word[0] && dfs(board, word, 1, point(r,c), visited))
return true;
visited[r][c] = false;
}
}
return false;
}
};
51. Word Search的更多相关文章
- [LeetCode] Word Search II 词语搜索之二
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- [LeetCode] Word Search 词语搜索
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- Leetcode: word search
July 6, 2015 Problem statement: Word Search Given a 2D board and a word, find if the word exists in ...
- Word Search I & II
Word Search I Given a 2D board and a word, find if the word exists in the grid. The word can be cons ...
- 【leetcode】Word Search
Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constr ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- 79. 212. Word Search *HARD* -- 字符矩阵中查找单词
79. Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be co ...
- 212. Word Search II
题目: Given a 2D board and a list of words from the dictionary, find all words in the board. Each word ...
- Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
随机推荐
- Rhel6-piranha配置文档
系统环境: rhel6 x86_64 iptables and selinux disabled 主机: 192.168.122.119 server19.example.com 192.168.12 ...
- MATLAB 图像操作基础
I = imread('pout.tif'); imshow(I): figure imhist(I) I2 = histeq(I); figure imshow(I2) imwrite (I2, ' ...
- fck编辑器漏洞想到的--目录的执行权限
结合近来我遇到的问题,转一篇关于目录的执行权限问题.来解决fck编辑器的漏洞.漏洞就是给人家上传了文件,而且还遍历目录或者直接执行文件,,非常大大的危险. 解决方法如下. 1,首先要删除fckedit ...
- json体会
1.用json-lib的jar包,创建JsonObject的对象(其引用取名jo),JsonObject jo = new JsonObject(); 再创建一个jsonobject对象:JsonOb ...
- css3简单介绍
关于css3我先介绍几个简单的选择器: 先进行设置: 字符串匹配属性选择器: E[alt^="a"] 选择属性中以a开头的元素: E[alt$="a"] 选 ...
- OC 解决NSArray、NSDictionary直接打印中文出现乱码的问题
在iOS开发中,经常需要查看数组中得元素是否是自己想要的,但是苹果并没有对直接打印数组中得中文作处理,直接打印就会出现一堆很讨厌的东西,解决其实很简单,就是需要通过为NSArray添加分类,重写 - ...
- ASP.NET Cookie存值问题
想必 用Cookie存值已经是很普遍的了,我也是刚学习了一点皮毛,现在就记下一点知识,便于日后翻阅. 1.C#代码存取Cookie值 //用Request获取到客户端Cookie 判断是否为空 if ...
- 超级链接a中javascript:void(0)弹出另外一个框问题
转字:http://my.oschina.net/castusz/blog/68186 结果在IE.Firefox.Chrome都是先执行的onclick事件,在项目中我们尽量不要同时使用这两种方式. ...
- having()方法设置查询条件,where()设置查询条件
having 和 where区别 ① 使用有先后顺序 ② where price>100 having price>100 ③ where 设置条件,字段必须是数据表中存在的 ...
- HDU 1254
http://acm.hdu.edu.cn/showproblem.php?pid=1254 暴搜,状态是四维的(箱子和人的坐标),向一个方向推箱子还要判断人能否走到推的位置,1A #include ...