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. Solr整合中文分词组件IKAnalyzer

    我用的Solr是4.10版本, 在csdn下载这个版本的IKAnalyzer:IK Analyzer 2012FF_hf1.zip 解压后目录如下: (1)这里还用solr自带的example实验分词 ...

  2. HTML特殊符号对照表

    特殊符号 命名实体 十进制编码 特殊符号 命名实体 十进制编码 Α Α Α Β Β Β Γ Γ Γ Δ Δ Δ Ε Ε Ε Ζ Ζ Ζ Η Η Η Θ Θ Θ Ι Ι Ι Κ Κ Κ Λ Λ Λ Μ ...

  3. 利用Jmeter进行Web测试

    JMeter介绍 脚本录制 运行JMeter进行测试 JMeter主要组件介绍 参数化设置 动态数据关联 使用命令行运行JMeter脚本 利用XSLT分析JMeter结果文件 1:JMeter,一个1 ...

  4. PureLayout和Masonry比较

    一年前那时我做iOS开发,为了自动布局适配多种屏幕,我一般使用Masonry,后来偶然地在一个视频教程中发现老师使用了UIView+Autolayout(现在作者改名为PureLayout)自动布局, ...

  5. Python的平凡之路(5)

    一.模块介绍 定义: 模块--用来从逻辑上组织python代码(变量,函数,类,逻辑:实现一个功能),本质就是.py结尾的python文件(文件名test.py,模块名test) 包—用来从逻辑上组织 ...

  6. CSS第一天总结

    CSS是层叠样式表,其作用在我看来就是统一一个或多个元素或者ID.class等的属性,CSS可以定义的属性非常多,一个好看的网页离不开CSS的修饰. CSS简而言之就是三个部分:选择符.属性.属性值. ...

  7. bsp STEP

    Web开发不仅现在比较流行,将来也会.我来谈一下最近bsp  application项目的体会吧,属初学者,请各位多多指教. SAP 的web开发方法有很多种,bsp只是其中一种,而bsp开发有可以分 ...

  8. 如何解决xx列不在表中

    在连接数据库的程序中常会出现xx列不在表中的问题?那么应该怎么解决呢? 产生此问题的原因有三种: 1.数据表没这个字段2.sql查询没将这个字段查出来3.字段名写错了 还有重要的是一定要检查你的数据库 ...

  9. 导出带图形的数据excel表

    public static string StatisticsSR(string parmStr) { try { StatisticsSRInfo parm = JsonConvert.Deseri ...

  10. css+div

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...