题目描述

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.

Example:

board =

[

['A','B','C','E'],

['S','F','C','S'],

['A','D','E','E']

]

Given word = "ABCCED", return true.

Given word = "SEE", return true.

Given word = "ABCB", return false.

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/word-search

代码实现

class Solution {
public:
bool existCore(vector<vector<char>>& board, string &word,int & Wordpos,int rows,int cols,int row,int col,vector<vector<int> > & visited){
if(Wordpos>=word.size())
return true;
if(col>=cols||row>=rows||col<0||row<0){
return false;
}
bool flag=false;
if(board[row][col]==word[Wordpos]&& visited[row][col]!=1){
++Wordpos;
visited[row][col]=1;
flag=existCore(board,word,Wordpos,rows,cols,row+1,col,visited)||existCore(board,word,Wordpos,rows,cols,row,col+1,visited)||existCore(board,word,Wordpos,rows,cols,row-1,col,visited)||existCore(board,word,Wordpos,rows,cols,row,col-1,visited);
if(!flag)
{
--Wordpos;
visited[row][col]=0;
}
}
return flag;
}
bool exist(vector<vector<char>>& board, string word) {
bool result=false;
if(board.size()==0||board[0].size()==0){
return false;
}
int rows=board.size();
int cols=board[0].size();
vector<vector<int> > visited;
for(int i=0;i<rows;++i){
vector<int> temp;
for(int j=0;j<cols;++j){
temp.push_back(0);
}
visited.push_back(temp);
}
int Wordpos=0;
for(int i=0;i<rows;++i){
for(int j=0;j<cols;++j)
{
if(existCore(board,word,Wordpos,rows,cols,i,j,visited))
return true;
}
}
return false;
}
};

总结

思路就是回溯法。中间纠结了一下,visited的数组是用二维数组还是用vector。后来发现如果用二维数组,传指针的地方较为麻烦,而且不能像vector一样直接visited[row][col]。

Leetcode79 Word Search的更多相关文章

  1. Leetcode79. Word Search单词搜索

    给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻"单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字 ...

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

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

  4. Leetcode: word search

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

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

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

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

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

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

随机推荐

  1. ASP.NET Core MVC 中的 Model 模型

    ASP.NET Core MVC 中的 Model 我们希望最终从 Student 数据库表中查询特定的学生详细信息并显示在网页上,如下所示. MVC 中的模型包含一组表示数据的类和管理该数据的逻辑. ...

  2. MACbook关机开机的咚咚咚声音

    在MAC系统中,设置→声音设置→内置扬声器静音即可. windows下面没有试过.

  3. [LeetCode] 457. Circular Array Loop 环形数组循环

    You are given a circular array nums of positive and negative integers. If a number k at an index is ...

  4. java byte[]和base64互相转换

      1.方式一 import java.io.UnsupportedEncodingException; import java.util.Base64; // byte[]转base64 Strin ...

  5. C# HTTP系列5 HttpWebResponse.StatusCode属性

    系列目录     [已更新最新开发文章,点击查看详细] HttpWebResponse.StatusCode 属性获取响应的状态.对应 HttpStatusCode 枚举值之一. HttpStatus ...

  6. Java8 新特性 Stream() 创建流

    通过Controllere类的Stream()和parallelStream()创建流 //通过集合创建流 @Test public void test1() { String arr[] = new ...

  7. WPF DataGrid 使用CellTemplateSelector 时SelectTemplate方法Item参数为NULL

    首先说明 在SelectTemplate中并Item参数并不是真的一直为Null.而是先执行空参数,之后再会执行有参数的. 至于原因 我也不知道... 具体验证过程是 也就说 做好非空检测即可

  8. C# 打开mpp文件(Microsoft object)问题总结

    有需求就有解决方案,早上还没有听说过什么是 mpp 文件,下午已经能成功的将功能实现,这难道就是程序员的职业素养?哈哈哈哈 从网上找了很多方法,最后自己找到一个十分简单的打开 mpp 文件的方法: p ...

  9. C#将运算字符串直接转换成表达式且计算结果

    DataTable dt = new DataTable(); var Result= dt.Compute("1+2*3+2", "");//将运算字符串转换 ...

  10. C# 文件监听类 FileSystemWatcher 属性

    属性: Path——这个属性告诉FileSystemWatcher它需要监控哪条路径.例如,如果我们将这个属性设为“C:Temp”,对象就监控那个目录发生的所有改变.IncludeSubDirecto ...