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.

思路:对每个方向递归调用,带回溯的DFS
class Solution {
public:
bool exist(vector<vector<char> > &board, string word) {
if(board.empty()) return false;
vector<vector<bool>> visited(board.size(), vector<bool>(board[].size(), false));
bool result = false;
for(int i = ; i < board.size(); i++)
{
for(int j = ; j < board[].size(); j++)
{
result = dfs(board,word,i,j,,visited);
if(result) return true;
visited[i][j] = false; //回溯
}
}
return false;
} bool dfs(vector<vector<char> > &board, string word, int i, int j, int depth, vector<vector<bool>> &visited)
{
if(board[i][j] != word[depth]) return false;
if(depth == word.length()-) return true;
visited[i][j] = true;
if(j < board[].size()- && !visited[i][j+]){ //向右
if(dfs(board,word, i, j+, depth+,visited)) return true;
else visited[i][j+] = false; //回溯
}
if(j > && !visited[i][j-]) //向左
{
if(dfs(board,word, i, j-, depth+,visited)) return true;
else visited[i][j-] = false; //回溯
}
if(i < board.size()- && !visited[i+][j]) //向下
{
if(dfs(board,word, i+, j, depth+,visited)) return true;
else visited[i+][j] = false; //回溯
}
if(i > && !visited[i-][j]) //向上
{
if(dfs(board, word, i-, j, depth+,visited)) return true;
else visited[i-][j] = false; //回溯
}
return false;
}
};
 
 

79. Word Search (Array; DFS,Back-Track)的更多相关文章

  1. 刷题79. Word Search

    一.题目说明 题目79. Word Search,给定一个由字符组成的矩阵,从矩阵中查找一个字符串是否存在.可以连续横.纵找.不能重复使用,难度是Medium. 二.我的解答 惭愧,我写了很久总是有问 ...

  2. [LeetCode] 79. 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] 79. 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 79. Word Search 、212. Word Search II

    https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...

  5. 【LeetCode】79. 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. LeetCode OJ 79. Word Search

    题目 Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fro ...

  7. Leetcode#79 Word Search

    原题地址 依次枚举起始点,DFS+回溯 代码: bool dfs(vector<vector<char> > &board, int r, int c, string ...

  8. LeetCode 79. Word Search(单词搜索)

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  9. LeetCode 79 Word Search(单词查找)

    题目链接:https://leetcode.com/problems/word-search/#/description 给出一个二维字符表,并给出一个String类型的单词,查找该单词是否出现在该二 ...

随机推荐

  1. win10下安装MySQL5.7.20

    1. 下载Mysql官方:http://www.mysql.com→downloads→选社区版本MySQL Community Edition(GPL)→点击Community(GPL)Downlo ...

  2. XMU 1246

    http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1246 求区间内素数个数,经典问题,区间长度10^6,数的取值最多能到10^12(此题范围稍小) 用 ...

  3. java定时器无法自动注入的问题解析(原来Spring定时器可以这样注入service)

    近些日子在做一个项目,在项目中需要用到spring的定时任务,但是在操作数据库的时候发现总是不能正确的进行数据的插入,经过查看才发现:是因为service层不能够通过普通的方法注入到action,所以 ...

  4. arcotg_udc: exports duplicate symbol imx_usb_create_charger (owned by kernel)

    /********************************************************************************* * arcotg_udc: exp ...

  5. [UOJ300][CTSC2017]吉夫特

    uoj bzoj luogu sol 根据\(Lucas\)定理,\(\binom nm \mod 2=\binom{n\%2}{m\%2}\times\binom{n/2}{m/2}\mod 2\) ...

  6. test20181024 qu

    题意 分析 极似UVa11995 I Can Guess the Data Structure! 模拟即可,时间复杂度(--)\(O(n \log n)\) 旁边的L君:这题有两个坑点,我被卡了一次. ...

  7. centeros php 实战

    apache 默认安装路径 Fedora Core, CentOS, RHEL:ServerRoot              ::      /etc/httpdPrimary Config Fle ...

  8. StringUtils.isEmpty()和isBlank()的区别

    一.概述 两种判断字符串是否为空的用法都是在程序开发时常用的,相信不少同学在这种简单的问题上也吃过亏,到底有什么区别,使用有什么讲究,带着问题往下看. 二.jar包 commons-lang3-3.5 ...

  9. DBUnit使用介绍

    一.DbUnit设计理念熟悉单元测试的开发人员都知道,在对数据库进行单元测试时候,通常采用的方案有运用模拟对象(mock objects)和stubs两种.通过隔离关联的数据库访问类,比如JDBC的相 ...

  10. thinkphp配置rewrite模式访问时不生效 出现No input file specified解决方法

    使用thinkphp配置rewire模式的路径访问网站时, 直接复制官网的.htaccess文件的代码复制过去 1 2 3 4 5 6 <IfModule mod_rewrite.c>   ...