[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 ...
- 华为ensp工具栏丢失解决方法
电脑是win8系统 不知道什么原因,华为模拟器的工具栏神奇的消失了,感觉很郁闷,每次要写字的时候都找不到在哪里(菜单里也没有),于是在官方论坛里面找了一下终于找出原因了. 关闭ensp,点击属性,进入 ...
- wlr设置 Blog Ping
ref:http://www.cnblogs.com/zhangyang/archive/2011/07/22/2113856.html 设置 Blog Ping 1.什么是Ping服务(Ping S ...
- Git-Git克隆
鸡蛋不装在一个篮子里 Git的版本库目录和工作区在一起,因此存在一损俱损的问题,即如果删除一个项目的工作区,同时也会把这个项目的版本库删除掉.一个项目仅在一个工作区中维护太危险了,如果有两个工作区就会 ...
- 8,实例化Flask的参数 及 对app的配置
Flask 是一个非常灵活且短小精干的web框架 , 那么灵活性从什么地方体现呢? 有一个神奇的东西叫 Flask配置 , 这个东西怎么用呢? 它能给我们带来怎么样的方便呢? 首先展示一下: from ...
- JAVA中使用AES加密解密
技术交流群: 233513714 /** * AES加密测试 * * @param str 加密参数 */ public void aesTest(String str) { log.info(&qu ...
- python 三目运算
python中的三目运算: result = a if condition else b #当满足condition返回a否则返回b 三目运算可以使你的代码看起来简洁,且运算高效
- Maven学习 (三) 使用m2eclipse创建web项目
1.首先确认你的eclipse已经安装好m2eclipse的环境,可以参照上两篇Maven学习内容 2.新建一个maven的项目 3.下一步默认配置,使用默认的工作空间,或者你可以自己选择其他的空间 ...
- NOIP 2018 总结
NOIP 2018 总结 提高组: 应得分 \(100 + 100 + 40 + 100 + 50 + 44 = 434\). 考后期望得分 \(100 + 100 + 20 + 100 + 50 + ...
- 自动化测试学习之路--json、dom编程
1.json: json是JavaScript Object Notation,是一种数据传输格式. 以下程序都是在浏览器的Console下执行的. 创建一个javaScript的对象: var st ...