Leetcode 79
//这是我写过最难的递归了。。。
//
class Solution {
public:
bool exist(vector<vector<char>>& board, string word) {
int m = board.size();
int n = board[].size();
for(int i=;i < m;i++){
for(int j=;j < n;j++)
if(DFS(board,word,,i,j)) return true;
}
return false;
}
// DFS搜索从x,y开始是否有word。
bool DFS(vector<vector<char>>& board,string word,int pos,int x,int y){
int m = board.size();
int n = board[].size();
int res;
if(pos == word.size())return true;//其实符合最后再需要一次递归调用才能被判断正确
if(x < ||y < ||x >= m||y >= n||board[x][y]!=word[pos]){//边界和字符都判断了很好。
return false;
}
else{
char s = board[x][y];//递归中常见的改变以后要还原
board[x][y] = '#';
res = DFS(board,word,pos+,x-,y)||DFS(board,word,pos+,x+,y)||DFS(board,word,pos+,x,y-)||DFS(board,word,pos+,x,y+);//搜索四周只要有一个是true,就是true
board[x][y] = s;
}
return res;
}
};
Leetcode 79的更多相关文章
- [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,这道走迷宫问题为什么不能用宽搜呢?
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode专题第48篇文章,我们一起来看看LeetCode当中的第79题,搜索单词(Word Search). 这一题官方给的难 ...
- leetcode@ [79/140] Trie树应用 Word Search / Word Search II
https://leetcode.com/problems/word-search/ class Solution { public: struct Trie{ Trie *next[]; bool ...
- 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类型的单词,查找该单词是否出现在该二 ...
- [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 ...
- Java实现 LeetCode 79 单词搜索
79. 单词搜索 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻"单元格是那些水平相邻或垂直相邻的单元格. ...
- [LeetCode] 79. 单词搜索(DFS,回溯)
题目 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻"单元格是那些水平相邻或垂直相邻的单元格.同一个单元格 ...
- Leetcode#79 Word Search
原题地址 依次枚举起始点,DFS+回溯 代码: bool dfs(vector<vector<char> > &board, int r, int c, string ...
随机推荐
- C/C++之内存分配
一.编译时与运行时的内存情况1.编译时不分配内存编译时是不分配内存的.此时只是根据声明时的类型进行占位,到以后程序执行时分配内存才会正确.所以声明是给编译器看的,聪明的编译器能根据声明帮你识别错误.2 ...
- Python入门之logging日志模块以及多进程日志
本篇文章主要对 python logging 的介绍加深理解.更主要是 讨论在多进程环境下如何使用logging 来输出日志, 如何安全地切分日志文件. 1. logging日志模块介绍 python ...
- 05: MySQL高级查询
MySQL其他篇 目录: 参考网站 1.1 GROUP BY分组使用 1.2 mysql中NOW(),CURDATE(),CURTIME()的使用 1.3 DATEDIFF() 函数 1.4 DATE ...
- 20145309李昊 WEB基础实践
本实验同学帮助下完成 实验问题回答 1.什么是表单 表单在网页中主要负责数据采集功能 一个表单有三个基本组成部分: 表单标签 表单域:包含了文本框.密码框.隐藏域.多行文本框.复选框.单选框.下拉选择 ...
- CAN/J1850/
(1)CAN:(差分信号)有信号CANH=3.5V,CANL=1.5V, 没有信号CANH=2.5V,CANL=2.5V 速率:CAN系统又分为高速和低速,高速CAN系统采用硬线是动力型,速度:500 ...
- Python3基础 getatime getctime getmtime 文件的最近访问 + 属性修改 + 内容修改时间
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- linux下的下载之道
a.场景: 平时会使用百度网盘下载电影,但是用ios版的百度云实在是有点慢,而平时不用windows,因此只能在linux上寻找解决之道. b.背景(我正在使用中): linux:ubuntu 14. ...
- luogu P4396 [AHOI2013]作业
目录 题目 思路 错误&&傻叉 代码 题目 luogu 思路 每次都是插入比之前所有数字大的数,所以之前的答案就不会改变 用fhq-treap求出原序列,然后用树状数组依次算出每个值得 ...
- Adobe Reader 2019 Offline Installer, Free Download - Best PDF Reader
https://ridnt-b.blogspot.com/2018/01/adobe-reader-2018-free-download.html http://ardownload.adobe.co ...
- jquery 之 extend的实现
function getOpt(target, obj1, obj2, obj3){ $.extend(target, obj1, obj2, obj3); return target; } var ...