题目描述

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. 【转】用C语言实现FFT算法

    傅里叶变换 快速傅里叶变换(Fast Fourier Transform,FFT)是一种可在  时间内完成的离散傅里叶变换(Discrete Fourier transform,DFT)算法. 在算法 ...

  2. 数论问题(1) : poj 1061

    最近,本人发现了一个新网站poj(不算新) 当然了,上面的资源很好...... 就是还没搞清楚它的搜索该怎么弄,如果有大佬能教教我怎么弄,请在下方留言 闲话少说,回归我们的正题 题目转自poj 106 ...

  3. Salesforce 开发整理(十)项目部署总结

    项目部署顺序 全局值集 小组 自定义字段-对象-设置(SF1 紧凑布局要和记录类型在这里要一起部署) 邮件模板-静态资源 角色 工作流-流定义(包含进程生成器) 批准过程 开发部署<Apex类, ...

  4. 初探Java设计模式4:一文带你掌握JDK中的设计模式

    转自https://javadoop.com/post/design-pattern 行为型模式 策略模式 观察者模式 责任链模式 模板方法模式 状态模式 行为型模式总结 本系列文章将整理到我在Git ...

  5. Linux命令随手记

    随手记录常用的Linux命令. tar 解压.   tar   -xzvf tar 压缩:tar   -czvf   .tgz (z是压缩格式,x为解压,v为显示过程,f指定备份文件) tar -zc ...

  6. python3 字符和数字(ASC码)转换

    print(ord('b')) print(ord('B')) print(chr(98)) print(chr(66)) 结果:98 66 b B 也可以数字转ASC码,原理一样,如下(结果就不输出 ...

  7. ISO C语言新标准(C11)

    新特性[2]有些和C++11是对应的,如线程和UTF-8: 对齐处理(Alignment)的标准化(包括_Alignas标志符,alignof运算符, aligned_alloc函数以及<std ...

  8. RecyclerView预览数据

    我们在布局文件里定义RecyclerView时,可以使用tools属性预览数据,如下: <android.support.v7.widget.RecyclerView android:layou ...

  9. laravel 一些好用的GitHub项目包

    链接地址:好用的GitHub包

  10. WPF 精修篇 路径动画

    原文:WPF 精修篇 路径动画 路径动画 是让一个对象围绕指定Path 的运动路径 进行移动的动画 举栗子 路径动画 使用 Blend 来设置 是十分简单的 首先用工具 笔  点出一条线 新建一个圆形 ...