79. Word Search (Array; DFS,Back-Track)
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.
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)的更多相关文章
- 刷题79. Word Search
一.题目说明 题目79. Word Search,给定一个由字符组成的矩阵,从矩阵中查找一个字符串是否存在.可以连续横.纵找.不能重复使用,难度是Medium. 二.我的解答 惭愧,我写了很久总是有问 ...
- [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 ...
- [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 ...
- leetcode 79. Word Search 、212. Word Search II
https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...
- 【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 ...
- 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 ...
- Leetcode#79 Word Search
原题地址 依次枚举起始点,DFS+回溯 代码: bool dfs(vector<vector<char> > &board, int r, int c, string ...
- 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 ...
- LeetCode 79 Word Search(单词查找)
题目链接:https://leetcode.com/problems/word-search/#/description 给出一个二维字符表,并给出一个String类型的单词,查找该单词是否出现在该二 ...
随机推荐
- 2018.12.4 王二的经济学故事 DYNASTIES
1从王二卖粮食:外汇储备缩水 2王二兑酒:固定汇率与变动汇率 3苹果换梨子:固定汇率的代价 4跨港购物:一价定律,汇率的价格传递效应 5富人吃透,春运火车票涨价:供需问题,弱者保护,让富人多消费,给穷 ...
- eclipse加入c标签
在MyEclipse中使用jstl标签只需导读jstl.jar就能使用,但是在Eclipse中还需要一点小套路 步骤: 一.导入jstl.jar 二.导入导入standard.jar 三.在WEB-I ...
- ubuntu16.04 源码安装Python3.7 (可以在此基础上安装Tensorflow) (确保Tensorflow计算框架与系统的彻底隔离)
Python3.7 源码下载: https://www.python.org/downloads/release/python-370/ 解压源码: tar -zxvf Python-3.7.0.tg ...
- InputStream,InputStreamReader和Reader的关系
InputStream:得到的是字节输入流,InputStream.read("filename")之后,得到字节流 Reader:读取的是字符流 InputStreamReade ...
- ASP.NET MVC3默认提供了11种ActionResult的实现
在System.Web.Mvc命名空间 ActionResult ContentResult EmptyResult FileResult HttpStatusCodeResult HttpNot ...
- cratedb 基本试用
安装 docker run -d -p 4200:4200 crate UI访问 http://localhost:4200/#!/ 创建数据 tweets 是默认导入的,点击帮助导航可以操作 登陆 ...
- bzoj2750最短路计数
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2750 枚举每一个起点,通过该边的子树中有多少节点就知道本次它被经过几次了: 因为同一起点到该 ...
- Window平台下React Native 开发环境搭建
1. 安装Node.js 2. 安装react-native-cli 命令行工具 npm install -g react-nativew-cli 3. 创建项目 $ react-native ini ...
- 华硕主板P8H61(P8H61-M_LX3_PLUS_R2.0)成功禁用USB口
公司大批这个型号的主板,在百度上搜索了一下,其中有一篇帖子说华硕客服说这个型号的USB控制XX是集成成南桥上面没法禁止. 经过研究发现官网上的0802版可以支持禁止usb,并且可以根据需要为每一个US ...
- inline修饰虚函数的问题
虚函数是否可以内联? 一般来说,inline是编译时的行为,虚函数是在程序执行时的行为,因此编译器一般会拒绝对虚函数进行内联!