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. 201621123006 《Java程序设计》第8周学习总结

    1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 2. 书面作业 ArrayList代码分析 1.1 解释ArrayList的contains源代码 源代码如下: 由源代码可 ...

  2. 互评Alpha版本

    作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2323] 队名:二次元梦之队 组长:刘莹莹 组员:周昊 潘世维  王玉潘 赵美增 ...

  3. mysql数据添加时如果这条数据存在进行修改

    1.建表 CREATE TABLE vipMovie( id INT PRIMARY KEY AUTO_INCREMENT, md_name VARCHAR(255) NOT NULL UNIQUE, ...

  4. 第8课 goto和void分析

    遭人遗弃的goto: C语言是一种面向过程的结构化语言,其中主要结构有三种,顺序执行.选择执行.循环执行.再复杂的程序也是由这三种结构组合而成的. goto破坏了结构化特性,使程序以第四种方式执行,结 ...

  5. Element-ui实现loading的局部刷新

    后台管理系统loading的局部刷新 在一次vue+element-ui后台管理系统的项目中,遇到这样一个问题,引入element-ui加载框后,loading会占满整个屏幕,虽然通过改变路由实现了局 ...

  6. HDU2874Connections between cities( LCA )Tarjan

    Problem Description After World War X, a lot of cities have been seriously damaged, and we need to r ...

  7. APScheduler - Advanced Python Scheduler

    简介 APScheduler:强大的任务调度工具,可以完成定时任务,周期任务等,它是跨平台的,用于取代Linux下的cron daemon或者Windows下的task scheduler. 内置三种 ...

  8. centOS6.6环境下安装AMP

    LAMP --  Linux Apache MySQL PHP 在CentOS安装的顺序,我一般是Apache -> MySQL -> PHP 第一步.安装并配置Apache 1.使用yu ...

  9. Java 8 Lambda表达式之方法引用 ::双冒号操作符

    双冒号运算符就是java中的方法引用,方法引用的格式是类名::方法名. 这里只是方法名,方法名的后面没有括号“()”.--------> 这样的式子并不代表一定会调用这个方法.这种式子一般是用作 ...

  10. Map和Bean的相互转换

    Map和Bean的相互转换 BeanUtils位于org.apache.commons.beanutils.BeanUtils下面,其方法populate的作用解释如下: 完整方法: BeanUtil ...