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 ...
随机推荐
- mysql修改Truncated incorrect DOUBLE value:
UPDATE shop_category SET name = 'Secolul XVI - XVIII' AND name_eng = '16th to 18th centuries' WHERE ...
- Educational Codeforces Round 21 Problem F (Codeforces 808F) - 最小割 - 二分答案
Digital collectible card games have become very popular recently. So Vova decided to try one of thes ...
- win32 自定义右键菜单
/**************************************************************************** 几大主要问题: 1.通过处理WM_MOUSE ...
- 前向算法Python实现
前言 这里的前向算法与神经网络里的前向传播算法没有任何联系...这里的前向算法是自然语言处理领域隐马尔可夫模型第一个基本问题的算法. 前向算法是什么? 这里用一个海藻的例子来描述前向算法是什么.网上有 ...
- Python3基础 函数 局部与全局变量同名,各管各的
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- linux中线程池【转】
本文转载自:http://blog.csdn.net/yusiguyuan/article/details/18401277 一.线程池 大多数的网络服务器,包括Web服务器都具有一个特点,就是单位时 ...
- 完整的Android开发环境Eclipse+ADT+SDK(22.0.1)
现在开始学习Android嵌入式编程,首要的问题就是在Windows中搭建开发环境,就这个都要摸索很长的时间,总是在版本之间折腾折腾去,而且Google的Android正式差劲得很,经常是连不上,要不 ...
- C Looooops(扩展欧几里得)题解
A Compiler Mystery: We are given a C-language style for loop of type for (variable = A; variable != ...
- MySQL中查询所有数据库占用磁盘空间大小和单个库中所有表的大小的sql语句
查询所有数据库占用磁盘空间大小的SQL语句: ,),' MB') as data_size, concat(,),'MB') as index_size from information_schema ...
- 筛选出sql 查询结果中 不包含某个字符
select * from table1 where patindex('%关键字%' , aa) = 0 select * from table1 where charindex('关键字' , a ...