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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. Leetcode: word search

    July 6, 2015 Problem statement: Word Search Given a 2D board and a word, find if the word exists in ...

  4. 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 ...

  5. 【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 ...

  6. 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 ...

  7. 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 ...

  8. 212. Word Search II

    题目: Given a 2D board and a list of words from the dictionary, find all words in the board. Each word ...

  9. Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

随机推荐

  1. Wince 6.0 窗口最大化显示

    在InitDialog用如下代码实现: CRect   m_FullScreenRect;   //全屏区域 CRect   WindowRect; GetWindowRect(&Window ...

  2. IIS 7.5 高并发参数配置

    IIS 7.5 高并发参数配置 由于之前使用的是默认配置,服务器最多只能处理5000个同时请求,对于高并发请求,参照文档设置10万并发 1. 调整IIS 7应用程序池队列长度 由原来的默认1000改为 ...

  3. Android FM模块学习之四源码分析(3)

    接着看FM模块的其他几个次要的类的源码.这样来看FM上层的东西不是太多. 请看android\vendor\qcom\opensource\fm\fmapp2\src\com\caf\fmradio\ ...

  4. cut - 小巧的文本截取工具

    简介 cut命令是Unix下的一个命令行程序.cut命令是以行为单位来处理的.cut命令处理的是标准输入,所以可以结合管道来进行文本的处理. 命令格式 cut option… [file]… cut命 ...

  5. 如何在JBoss WildFly 8 自定义log4j日志

    最近在 JBoss WildFly 8 下部署 Web应用,自定义的 log4j 日志不工作.console下无日志输出,用System.out.println都不输出内容到console. 原因是J ...

  6. Web Storage的方法

    1.分为两种:localStorage与sessionStorage.2.存储形式:key-value的形式.sessionStorage 1.session定义:session指用户在浏览某个网站时 ...

  7. squid ACL 大全

    Access Controls in Squid Contents Access Controls in Squid The Basics: How the parts fit together AC ...

  8. java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

    因为同样的方法在java中没有问题,放到web工程中就出现了问题.网上查到的资料,说只要把mysql-connector-java-5.1.7-bin.jar放入tomcat中的lib文件夹就可以.很 ...

  9. 信号处理基础概念比较----频谱vs功率谱vs能谱

    频谱: 对动态信号在频率域内进行分析,分析的结果是以频率为坐标的各种物理量的谱线和曲线,可得到各种幅值以频率为变量的频谱函数F(ω).频谱是个很不严格的东西,常常指信号的Fourier变换.频谱分析中 ...

  10. knockout 学习实例5 style

    <!DOCTYPE html><html><head> <meta charset="utf-8"> <title>&l ...