LeetCode OJ--Word Search **
https://oj.leetcode.com/problems/word-search/
类似于在棋盘上走步,走过的地方不能再走,每次都可以走当前位置的上、下、左、右,问能不能走出要求的形状来。
深搜:
依次搜它的上
下
左
右
在深搜中,容易超时,所以如果有复杂类型的数据传值,一般都用引用。当然,为了恢复每次引用的现场,需要把本次深搜中改变的值,再改回来。
- class Solution {
- public:
- bool exist(vector<vector<char> > &board, string word) {
- if(board.size() == || word.size() == )
- return false;
- int row = board.size();
- int col = board[].size();
- vector<vector<bool> > flag;
- //initialize
- flag.resize(row);
- for(int i = ; i < row; i++)
- {
- flag[i].resize(col);
- for(int j = ; j < col; j++)
- flag[i][j] = false;
- }
- bool ans = false;
- for(int i = ; i < row; i++)
- {
- for(int j = ; j < col; j++)
- {
- if(board[i][j] == word[])
- {
- ans = find(board,word,i,j,flag,);
- if(ans)
- return true;
- }
- }
- }
- return false;
- }
- bool find(vector<vector<char> > &board, string &word, int i, int j,vector<vector<bool> > &flag, int match_index)
- {
- if(match_index == word.size())
- return true;
- //true means used
- flag[i][j] = true;
- bool ans;
- //up
- if(i!= && board[i-][j] == word[match_index] && flag[i-][j] == false)
- {
- ans = find(board,word,i-,j,flag,match_index + );
- if(ans)
- return true;
- }
- //right
- if(j!= board[].size() - && board[i][j+] == word[match_index] && flag[i][j+] == false)
- {
- ans = find(board,word,i,j+,flag,match_index + );
- if(ans)
- return true;
- }
- //down
- if(i!= board.size() - && board[i+][j] == word[match_index] && flag[i+][j] == false)
- {
- ans = find(board,word,i+,j,flag,match_index + );
- if(ans)
- return true;
- }
- //left
- if(j!= && board[i][j-] == word[match_index] && flag[i][j-] == false)
- {
- ans = find(board,word,i,j-,flag,match_index + );
- if(ans)
- return true;
- }
- flag[i][j] = false;
- return false;
- }
- };
LeetCode OJ--Word Search **的更多相关文章
- [LeetCode OJ] Word Search 深度优先搜索DFS
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- Java for LeetCode 212 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] 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] 212. Word Search II 词语搜索 II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- [LeetCode] 212. 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] 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】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——Word Ladder
http://oj.leetcode.com/problems/word-ladder/ 图的最短路径问题,可以用最短路径算法,也可以深搜,也可以广搜. 深搜版本: 第一次写的时候,把sum和visi ...
- [LeetCode#212]Word Search II
Problem: Given a 2D board and a list of words from the dictionary, find all words in the board. Each ...
随机推荐
- Python装饰器使用规范案例详解
由于函数也是一个对象,而且函数对象可以被赋值给变量,所以,通过变量也能调用该函数. >>> def now(): ... print('2015-3-25') ... >> ...
- Green Space【绿色空间】
Green Space Living in an urban area with green spaces has a long-lasting positive impact on people's ...
- The 2018 ACM-ICPC Asia Qingdao Regional Contest(青岛网络赛)
A Live Love 水 #include <algorithm> #include<cstdio> #include<cstring> using namesp ...
- HDU 6153 KMP
最终刷KMP目标就是为了挑战这道题!现在成功了恩... 首先,题目大意是:给出一个字符串str1,之后给出另一个字符串str2,问,str2的后缀在str1匹配的次数*后缀当前长度是多少 首先考虑正统 ...
- 扩展程序 - Google Chrome
Adblock Plus 3.0.3 Adblock Plus 已被超过 1 亿台设备使用,是世界上最受欢迎的广告拦截软件. ID:cfhdojbkjhnklbpkdaibdccddilifddb 查 ...
- 手机注册过哪些网站37kfenxi.com,查询注册过哪些网站
注册过哪些网站?发现这么一个网站,https://www.37kfenxi.com?_=cnblogs 可以根据手机号码查询注册过哪些网站,然后通过大数据分析出机主的性格,爱好等. 据说还可以查老板, ...
- 《鸟哥的Linux私房菜》学习笔记(5)——权限管理
一.权限的基本概念 权限:访问计算机资源或服务的访问能力. Linux中,每一个资源或者服务的权限, ...
- service-worker实践
service-worker虽然已列入标准,但是支持的浏览器还是有限制,还有比较多的问题. 1. 生命周期 注册成功-------installing--------------> 安装成功(i ...
- 设计模式之第17章-备忘录模式(Java实现)
设计模式之第17章-备忘录模式(Java实现) 好男人就是我,我就是曾小贤.最近陈赫和张子萱事件闹得那是一个沸沸扬扬.想想曾经每年都有爱情公寓陪伴的我现如今过年没有了爱情公寓总是感觉缺少点什么.不知道 ...
- cookie注意事项
cookie是在 HTTP 协议下,服务器或脚本可以维护客户工作站上信息的一种方式. 四种会话跟踪技术(URL重写.隐藏表单域.Cookie.Session) 服务端技术:URL重写,Session, ...