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 =

[
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
]

word = "ABCCED", -> returns true,
word = "SEE", -> returns true,
word = "ABCB", -> returns false.

分析: 刚开始做的时候有误解,实际上就是从矩阵中找一条线路求解出word dfs求解最方便,注意一下边界范围

class Solution {
private:
int rows;
int cols;
public:
bool exist(vector<vector<char>>& board, string word) {
rows = board.size();
cols = board[].size();
for(int i=; i< rows; i++)
for(int j=;j<cols; j++){
if(dfs(board,i,j,word,))
return true;
}
return false;
}
bool dfs(vector<vector<char>>& board, int x, int y,string word, int index){
//cout << x << " "<<y <<endl;
//cout << "index = "<<index<<endl;
if(x< || y< || x>=rows || y>=cols || (index<word.size() && board[x][y]!=word[index]) )
return false;
if(index+ == word.size()){
return true;
}
char c =board[x][y];
board[x][y] = '.';
if(dfs(board,x-,y,word,index+) || dfs(board,x+,y,word,index+)
|| dfs(board,x,y-,word,index+) || dfs(board,x,y+,word,index+))
return true; board[x][y] =c; return false;
}
};

  

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. 51. Word Search

    Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constr ...

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

  9. 212. Word Search II

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

  10. 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. 你真的会玩SQL吗?透视转换的艺术

    你真的会玩SQL吗?系列目录 你真的会玩SQL吗?之逻辑查询处理阶段 你真的会玩SQL吗?和平大使 内连接.外连接 你真的会玩SQL吗?三范式.数据完整性 你真的会玩SQL吗?查询指定节点及其所有父节 ...

  2. 代码的坏味道(9)——异曲同工的类(Alternative Classes with Different Interfaces)

    坏味道--异曲同工的类(Alternative Classes with Different Interfaces) 特征 两个类中有着不同的函数,却在做着同一件事. 问题原因 这种情况往往是因为:创 ...

  3. 介绍,介绍我的底层支持库 Net.Sz.CFramework

    Net.Sz.CFramework  是我自己的底层库,是经过验证的底层库. 包含: socket tcp协议,socket http协议线程池,线程模型,任务模型,定时器模型,日志模块脚本模块一些辅 ...

  4. python类的实例方法\静态方法\类方法区别解析(附代码)

    前言 搞了好久python,一直搞得不太清楚这几种类的方法,今天花时间好好测试一下,算是弄懂点皮毛吧. 三种方法的不同 先剽窃个图看一下 可以看到,实例是三种方法都可以调用的,而类只可以调用两种.所以 ...

  5. php内核分析(八)-zend_compile

    这里阅读的php版本为PHP-7.1.0 RC3,阅读代码的平台为linux 回到之前看的zend_eval_stringl ZEND_API int zend_eval_stringl(char * ...

  6. oracle运算符

    单引号('): 在Oracle中,应该只运用单引号将文本和字符和日期括起来,不能运用引号(包括单双引号)将数字括起来. 双引号("): 在Oracle中,单双引号意思不同.双引号被用来将包含 ...

  7. Lua BehaviourTree 各节点说明

    项目说明 本行为树的代码使用Lua编写,所有的内容也建立的Lua的基础语法之上 因为公司项目需求,需要一套Lua的行为树代码,所以尝试从饥荒中抽离了行为树相关的代码.绝大多数节点行为与饥荒中相同,不过 ...

  8. .NET正则表达式基础入门(四)

    断言 判断某个位置左侧或者右侧是否符合匹配.常见断言有三种,单词边界.行起始/结束位置.环视.阅读本章前,建议先下载我于CSDN上传的示例代码,下载无需分数,下载链接. 1.单词边界 正则表达式&qu ...

  9. 关于c#在DataTable中根据条件删除某一行

    我们经常会将数据源放在DataTable里面,但是有时候也需要移除不想要的行,下面的代码告诉你们 DataTable dts:                DataRow[] foundRow;   ...

  10. java抽象类和接口

    面向对象设计过程中重要的一点是如何进行抽象,即把"问题空间"中的元素与"方案空间"中的元素建立理想的一对一的映射关系.抽象类和接口便是抽象过程中的产物.     ...