[LeetCode]Word Search 回溯
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
.
#include <iostream>
#include <vector>
#include <string>
using namespace std; class Solution {
public:
bool exist(vector<vector<char> > &board, string word) {
if(word.length()<) return true;
if(board.size()==||board[].size()==) return false;
for(int i =;i<board.size();i++){
for(int j =;j<board[].size();j++){
if(board[i][j]==word[]&&helpFun(board,word,,i,j))
return true;
}
}
return false;
} bool helpFun(vector<vector<char> >&board,string & word,int idx,int beg_i,int beg_j)
{
if(idx == word.size()) return true;
char tmp = board[beg_i][beg_j];
board[beg_i][beg_j] = '*';
if(beg_i>&&board[beg_i-][beg_j]==word[idx]&&helpFun(board,word,idx+,beg_i-,beg_j)){
board[beg_i][beg_j] = tmp;
return true;
}
if(beg_i<board.size()-&&board[beg_i+][beg_j]==word[idx]&&helpFun(board,word,idx+,beg_i+,beg_j)){
board[beg_i][beg_j] = tmp;
return true;
}
if(beg_j>&&board[beg_i][beg_j-]==word[idx]&&helpFun(board,word,idx+,beg_i,beg_j-)){
board[beg_i][beg_j] = tmp;
return true;
}
if(beg_j<board[].size()-&&board[beg_i][beg_j+]==word[idx]&&helpFun(board,word,idx+,beg_i,beg_j+)){
board[beg_i][beg_j] = tmp;
return true;
}
board[beg_i][beg_j] = tmp;
return false;
}
}; int main()
{
vector<vector< char> > board{{'A','B','C','E'},{'S','F','C','S'},{'A','D','E','E'}};
Solution sol;
cout<<sol.exist(board,"ABCB")<<endl;
return ;
}
[LeetCode]Word Search 回溯的更多相关文章
- [LeetCode] Word Search II 词语搜索之二
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- [LeetCode] 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: Word Search 解题报告
Word SearchGiven a 2D board and a word, find if the word exists in the grid. The word can be constru ...
- Leetcode: word search
July 6, 2015 Problem statement: Word Search Given a 2D board and a word, find if the word exists in ...
- LeetCode() Word Search II
超时,用了tire也不行,需要再改. class Solution { class TrieNode { public: // Initialize your data structure here. ...
- [leetcode]Word Search @ Python
原题地址:https://oj.leetcode.com/problems/word-search/ 题意: Given a 2D board and a word, find if the word ...
- [Leetcode] 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] Word Search [37]
题目 Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fro ...
- leetcode Word Search 待解决?
终于搞定了这个DFS,最近这个DFS写的很不顺手,我一直以为递归这种东西只是在解重构时比较麻烦,现在看来,连最简单的返回true和false的逻辑关系都不能说one hundred present 搞 ...
随机推荐
- Python入门必知的几个点
Python是Guido van Rossum在1989年圣诞节期间,为了打发无聊的圣诞节而编写的一个编程语言.全世界差不多有600多种编程语言,但流行的编程语言也就那么20来种.如果你听说过TIOB ...
- Windows Server 2008 R2 可能会碰到任务计划无法自动运行的解决办法
在做Windows Server 2008R2系统的计划任务时使用到了bat脚本,手动启动没问题,自动执行缺失败,代码:0x2. 将“操作”的“起始于”进行设置了bat脚本的目录即可.
- POJ:1703-Find them, Catch them(并查集好题)(种类并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 49867 Accepted: 153 ...
- 3,Linux入门
操作系统的分类 Windows系列操作系统,Unix类操作系统,Linux类操作系统,Mac操作系统 提问:为什么要去学习Linux? 同学甲可能要问,超哥你介绍了这么多有关Linux的知识,但我还是 ...
- Android stadio Switch repository Android stadio切换仓库
Android stadio 有时候,有很多module. 这些module 都有自己的仓库.也就是不在一块.那么,Android stadio 默认管理的就是根git. 如图,画对号的就是默认的. ...
- 《Cracking the Coding Interview》——第9章:递归和动态规划——题目10
2014-03-20 04:15 题目:你有n个盒子,用这n个盒子堆成一个塔,要求下面的盒子必须在长宽高上都严格大于上面的.如果你不能旋转盒子变换长宽高,这座塔最高能堆多高? 解法:首先将n个盒子按照 ...
- python中subprocess.Popen执行命令并持续获取返回值
先举一个Android查询连接设备的命令来看看Python中subprocess.Popen怎么样的写法.用到的命令为 adb devices. import subprocess order='ad ...
- 牛客网暑期ACM多校训练营(第一场):J-Different Integers(分开区间不同数+树状数组)
链接:J-Different Integers 题意:给出序列a1, a2, ..., an和区间(l1, r1), (l2, r2), ..., (lq, rq),对每个区间求集合{a1, a2, ...
- cloud-utils
官方下载:https://launchpad.net/cloud-utils rpm包下载地址:http://rpmfind.net/linux/rpm2html/search.php?query=c ...
- sources.ustc.debian
deb http://mirrors.ustc.edu.cn/debian/ jessie main contrib non-free deb-src http://mirrors.ustc.edu. ...