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类型的单词,查找该单词是否出现在该二 ...
随机推荐
- Scrum立会报告+燃尽图 06
作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2289] 版本控制:https://git.coding.net/liuyy08 ...
- MySQL 服务正在启动 .MySQL 服务无法启动。系统出错。发生系统错误 1067。进程意外终止。
MySQL 服务正在启动 .MySQL 服务无法启动.系统出错.发生系统错误 1067.进程意外终止. 检查了一个晚上才发现是---配置问题 #Path to installation directo ...
- java IO 学习(三)
java IO 学习(一)给了java io 进行分类,这一章学习这些类的常用方法 一.File 1.创建一个新的File的实例: /** * 创建一个新的File实例 */ File f = new ...
- bzoj 4570: [Scoi2016]妖怪 凸包
题目大意: http://www.lydsy.com/JudgeOnline/problem.php?id=4570 题解 我们知道如果一个怪物要取到攻击力的最大值,那么一定是把防御力都转化了 所以我 ...
- 写写Django中DRF框架概述以及序列化器对象serializer的构造方法以及使用
写写Django中DRF框架概述以及序列化器对象serializer的构造方法以及使用 一.了解什么是DRF DRF: Django REST framework Django REST framew ...
- Hive之 hive与rdbms对比
对比图 总结: Hive并非为联机事务处理而设计,Hive并不提供实时的查询和基于行级的数据更新操作.Hive是建立在Hadoop之上的数据仓库软件工具,它提供了一系列的工具,帮助用户对大规模的数据进 ...
- php在循环内外实例化类占用内存比较
关于php类的实例化和内存的关系,可以这么说:只要有一个new 关键字就是创建一个对象,创建一个对象就是在内存中分配了一个空间. 代码1: 在循环外实例化类:class ABC{ public $nu ...
- 纯php实现中秋博饼游戏(2):掷骰子并输出结果
这篇是纯php实现中秋博饼游戏系列博文(2) 上文是:纯php实现中秋博饼游戏(1):绘制骰子图案 http://www.cnblogs.com/zqifa/p/php-dice-1.html要纯ph ...
- CCFlow SDK模式开发(有比较详细的代码,以服务的形式与ccflow数据库进行数据交互)
http://www.cnblogs.com/s0611163/p/3963142.html 需求: 1.业务数据要保存在我们自己的数据库里 2.CCFlow有保存草稿的功能,但是领导要求每个 ...
- laravel Hash密码 校对
laravel加密 是使用hash不可逆的,但是可以对加密后的密码进行校对 $data = $r->all();$id = $data['id'];$user_password = bcrypt ...