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. 将Web项目War包部署到Tomcat服务器基本步骤(完整版)

    1. 常识:   1.1 War包 War包一般是在进行Web开发时,通常是一个网站Project下的所有源码的集合,里面包含前台HTML/CSS/JS的代码,也包含Java的代码. 当开发人员在自己 ...

  2. Fire Game FZU - 2150 (bfs)

    Problem 2150 Fire Game Accept: 3772    Submit: 12868Time Limit: 1000 mSec    Memory Limit : 32768 KB ...

  3. B1023 组个最小数 (20分)

    B1023 组个最小数 (20分) 给定数字 0-9各若干个.你可以以任意顺序排列这些数字,但必须全部使用.目标是使得最后得到的数尽可能小(注意 0 不能做首位).例如:给定两个 0,两个 1,三个 ...

  4. Codeforces:68A-Irrational problem(暴力大法好)

    A- Irrational problem p Time Limit: 2000MS Memory Limit: 262144K 64bit IO Format: %I64d& %I64 De ...

  5. Kinect安装

    在连接kinect机器前,需要先安装两个软件,而在安装这两个软件前需要有vs2010(专业版本和快速版),因为需要包含.net framework 4.0 kinect sdk http://www. ...

  6. cglib动态代理之原理说明

    cglib采用了非常底层的字节码技术,通过目标类的字节码,为目标类创建子类,并在子类中用方法拦截技术,拦截所有父类方法的调用,并对拦截方法进行增强. 1)底层采用字节码框架ASM,来转换字节码来生成新 ...

  7. IOS笔记046-UIApplication/导航控制器

    UIApplication 每一个应用都有自己的UIApplication对象,而且是单例的 通过[UIApplication sharedApplication]可以获得这个单例对象 一个iOS程序 ...

  8. php代码审计 strcmp和MD5函数漏洞

    通过get得到三个值,v1,v2,v3. if第一层判断,v1和v2得到的值不一样,但是对它们进行md5加密后的值得相等. if第二层判断,v3得到的值得和$flag的值相等,满足这两个条件输出fla ...

  9. Goole Search Auto Complete

    这个项目就九章算法大数据课程的一个项目.主要分为两步: 第一步是 offline 建立 数据库 我们用两个map reduce 的data pipline 来实现. 第二步是 online显示把数据里 ...

  10. 【转】iTween for Unity

    http://www.cnblogs.com/zhaoqingqing/p/3833321.html?utm_source=tuicool&utm_medium=referral 你曾经在你的 ...