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 **的更多相关文章

  1. [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 ...

  2. 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 ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. leetcode 79. Word Search 、212. Word Search II

    https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...

  8. 【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 ...

  9. LeetCode OJ——Word Ladder

    http://oj.leetcode.com/problems/word-ladder/ 图的最短路径问题,可以用最短路径算法,也可以深搜,也可以广搜. 深搜版本: 第一次写的时候,把sum和visi ...

  10. [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 ...

随机推荐

  1. vue之神奇的动态按钮

    今天我们将利用vue的条件指令来完成一个简易的动态变色功能按钮 首先我们还是要对vue进行配置,在上篇随笔中有相关下载教学. 然后我们要在html页面上搭建三个简易的按钮,颜色分别为紫,绿和蓝(颜色随 ...

  2. mem_init()

    原本由bootmem管理的内存在mem_init函数中交由伙伴系统管理. 1.free_unused_memmap_node 相邻的membank间可能存在空洞,但在bootmem阶段这些空洞页也分配 ...

  3. BZOJ - 2744 朋友圈 (二分图上的最大团)

    [题目大意] 在很久很久以前,曾经有两个国家和睦相处,无忧无虑的生活着.一年一度的评比大会开始了,作为和平的两国,一个朋友圈数量最多的永远都是最值得他人的尊敬,所以现在就是需要你求朋友圈的最大数目.两 ...

  4. CCPC_1005

    可怕.....的提.....显而易见的规律活活没照出来...不过说起来却是不能严格证明....于是...脑筋急转弯活活猜不出来..... 1*1->1*2->2*2->2*3-> ...

  5. 查询语句为“%string_”的情况

    select * from t_user where user_name like '%Joe_%'实际查询出来的语句为: 而不像预计的前两条.

  6. HDU 4628 Pieces(状态压缩+记忆化搜索)

    http://acm.hdu.edu.cn/showproblem.php?pid=4628 题意:给个字符窜,每步都可以删除一个字符窜,问最少用多少步可以删除一个字符窜分析:状态压缩+记忆化搜索  ...

  7. loj2292 「THUSC 2016」成绩单

    ref 我是傻逼,我啥也不会,这是我抄的. #include <iostream> #include <cstring> #include <cstdio> usi ...

  8. python安装pattern失败

    做文本分类需要用到pattern.en进行词形还原,安装了一上午都没有成功,mysqlclient安装失败.最后解决办法,pip install --only-binary :all: mysqlcl ...

  9. Struts2请求流程

    1. 一个请求在Struts2框架中的处理步骤: a) 客户端初始化一个指向Servlet容器的请求: b) 根据Web.xml配置,请求首先经过ActionContextCleanUp过滤器,其为可 ...

  10. golang 高级

    下面的EmployeeByID函数将根据给定的员工ID返回对应的员工信息结构体的指针.我们可以使用点操作符来访问它里面的成员: func EmployeeByID(id int) *Employee ...