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 ...
随机推荐
- 线程之sleep(),wait(),yield(),join()等等的方法的区别
操作线程的常用方法大体上有sleep(),join(),yield()(让位),wait(),notify(),notifyAll(),关键字synchronized等等. 由于这些方法功能有些 ...
- 动态规划:HDU1069-Monkey and Banana
Monkey and Banana Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- 阿里云服务器+Tomcat项目+mysql 发布项目全过程
这个博客管理系统折腾我好几天了. 总结一下整个过程吧! 1.首先这个博客在tomcat下 windows系统可以完全跑起来了,无论是前台或者后台都能实现所有的功能. 2.然后我买了一个域名jasonj ...
- itchat 总结(转)
python实现微信接口(itchat) 安装 sudo pip install itchat 登录 itchat.auto_login() 这种方法将会通过微信扫描二维码登录,但是这种登录的方式确实 ...
- 【面试】一篇文章帮你彻底搞清楚“I/O多路复用”和“异步I/O”的前世今生
曾经的VIP服务 在网络的初期,网民很少,服务器完全无压力,那时的技术也没有现在先进,通常用一个线程来全程跟踪处理一个请求.因为这样最简单. 其实代码实现大家都知道,就是服务器上有个ServerSoc ...
- bootstrap button
样式修改 .sign-button, .sign-button:hover, .sign-button:focus, .sign-button:active, .sign-button:visited ...
- Django 四——ModelForm用法
内容概要: 1.新增数据库表中数据 2.更新数据库表中数据 Django的ModelForm Django中内置了Form和Model两个类,有时候页面的表单form类与Model类是一一对应,因此分 ...
- 云容器和安全性仍然是困扰IT人士的头号问题
[TechTarget中国原创] 容器和云安全仍然是IT领域中最热门的两个话题.下面就让我们来详细探讨一下吧. 云容器风靡一时是事出有因的.如Docker这样的容器能够提高应用的可移植性,并让企业用户 ...
- adb命令模拟按键事件
//这条命令相当于按了设备的Backkey键 adb shell input keyevent 4 //可以解锁屏幕 adb shell input keyevent 82 //在屏幕上做划屏操作, ...
- python - 接口自动化测试 - TestRecharge - 充值接口测试用例
# -*- coding:utf-8 -*- ''' @project: ApiAutoTest @author: Jimmy @file: test_recharge.py @ide: PyChar ...