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面向对象之反射和内置方法
一.静态方法(staticmethod)和类方法(classmethod) 类方法:有个默认参数cls,并且可以直接用类名去调用,可以与类属性交互(也就是可以使用类属性) 静态方法:让类里的方法直接被 ...
- Python_day01_作业笔记
内容大纲: 1. python的出生与应用以及历史, python2x: 源码冗余,源码重复,源码不规范. python3x: 源码清晰优美简单. 2. python的种类. Cpython: 官 ...
- 基于neo4j图数据库,实现人口关系大图的基本思路及实现方案。
近期由于工作需要,需要做一个人口关系大图的存储及检索方案,我们主要的数据对象有:人口(年龄,身份证号码,性别..) :学校信息(学校地址,学校名称,学校级别,学校下边的年级班级..):就职信息(公司名 ...
- [USACO]奶牛赛跑(逆序对)
Description 约翰有 N 头奶牛,他为这些奶牛准备了一个周长为 C 的环形跑牛场.所有奶牛从起点同时起跑,奶牛在比赛中总是以匀速前进的,第 i 头牛的速度为 Vi.只要有一头奶牛跑完 L 圈 ...
- 4 Template层-验证码
1.验证码 在用户注册.登录页面,为了防止暴力请求,可以加入验证码功能,如果验证码错误,则不需要继续处理,可以减轻一些服务器的压力 使用验证码也是一种有效的防止crsf的方法 验证码效果如下图: 官网 ...
- cakephp 中Console / Shell 有什么优点?
Which is the advantage of using CakePHP Console / Shell for programmed tasks ? 查看原文 最近用到了cakephp中的sh ...
- flex遭遇text-overflow:hidden,white-space:nowrap
最近在项目中遇到使用flex的时候,在flex-item元素中使用text-overflow:hidden:white-space:nowrap:进行省略文字的操作. 发现flex-item失控了,长 ...
- 如何使用PowerShell管理Windows服务
[TechTarget中国原创] 作为一名系统管理员,最常见的任务之一就是学会管理Windows服务,这是保证Windows服务器和客户端正常运行的重要内容. 许多操作系统和应用程序都依赖于这些服务. ...
- Monkey log异常分析说明
以下主要针对在Android-Phone项目中进行Monkey log进行分析和说明,可以对bug提交作为参考. 要求熟悉,应用的包名.也就是说那个应用包出现问题,该属于那个模块,应用包名是判断依据. ...
- stdlib.h中自带的两个算法qsort,bsearch
http://zh.cppreference.com/w/c/algorithm ========== void qsort( void *ptr, size_t count, size_t size ...