Given a 2D board and a word, find if the word exists in the grid.

The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.

For example,
Given board =

[
["ABCE"],
["SFCS"],
["ADEE"]
]

word = "ABCCED", -> returns true,
word = "SEE", -> returns true,
word = "ABCB", -> returns false.

典型的搜索问题,这道题目用了两种方法来做。

一种用到动态规划的思想,用一个vector记录下word中前i个字符构成的子串的所有搜索路径,然后对于前i+1个字符构成的子串

查找搜索路径时,只需判断第i+1个字符在当前由前i个字符构成的子串的搜索路径的最后一个位置的上下左右四个方向是否出现,

如果出现则保存新的路径。最后只需判断查找word时是否有可行的搜索路径存在,如果有,则返回true,否则,返回false。但是,

这种方法的复杂度很高,对于大的测试数据,出现TLE。

 class Solution {
public:
bool exist(vector<vector<char> > &board, string word) {
map<int, vector<vector<unsigned> > > path; //path[i]表示word中的前i个字符构成的字符串在board中的查找路径,一共有path[i].size()条合法查找路径
if(word.size()==)
return true;
int width = board[].size();
for(unsigned i=; i<board.size()*width; i++)
{
if(board[i/width][i%width]==word[])
{
vector<unsigned> start;
start.push_back(i);
path[].push_back(start);
}
}
for(unsigned i=; i<word.size(); i++)
{
char c = word[i];
//vector<vector<unsigned> > pt = path[i-1]; for(vector<vector<unsigned> >::iterator it=path[i].begin(); it!=path[i].end(); it++)
{
vector<unsigned> unipath = *it; int lastpos = unipath.back(); if((lastpos-width)>= && board[(lastpos-width)/width][(lastpos-width)%width]==c && find(unipath.begin(), unipath.end(), (lastpos-width))==unipath.end())
{
unipath.push_back(lastpos-width);
path[i+].push_back(unipath);
unipath.pop_back();
}
if((lastpos+width)<(board.size()*width) && board[(lastpos+width)/width][(lastpos+width)%width]==c && find(unipath.begin(), unipath.end(), (lastpos+width))==unipath.end())
{
unipath.push_back(lastpos+width);
path[i+].push_back(unipath);
unipath.pop_back();
}
if(lastpos%width> && board[(lastpos-)/width][(lastpos-)%width]==c && find(unipath.begin(), unipath.end(), (lastpos-))==unipath.end())
{
unipath.push_back(lastpos-);
path[i+].push_back(unipath);
unipath.pop_back();
}
if((lastpos%width)<(width-) && board[(lastpos+)/width][(lastpos+)%width]==c && find(unipath.begin(), unipath.end(), (lastpos+))==unipath.end())
{
unipath.push_back(lastpos+);
path[i+].push_back(unipath);
unipath.pop_back();
}
if(i==word.size()- && path[word.size()].size()!=)
return true;
}
}
if(path[word.size()].size()==)
return false;
else
return true;
}
};

第二种是用到DFS(深度优先搜索),先找到可行的起始点,然后对于每个起始点判断是否有一条合法搜索路径存在,如果找到

一条合法搜索路径,则不进行后续搜索,直接返回true。如果所有可能的路径验证都不成立后,最后返回false。测试AC。

 void search(vector<vector<char> > &board, string word, vector<int> &path, bool &flag)
{
if(word.size()==)
{
flag = true;
return;
} int width = board[].size();
char c = word[];
if(path.size()==)
{
for(unsigned i=; i<board.size()*width; i++)
{
if(board[i/width][i%width]==c)
{
path.push_back(i);
search(board, word.substr(), path, flag);
if(flag==true)
return;
path.pop_back();
}
}
}
else
{
int lastpos = path.back();
if((lastpos-width)>= && board[(lastpos-width)/width][(lastpos-width)%width]==c && find(path.begin(), path.end(), (lastpos-width))==path.end())
{
path.push_back(lastpos-width);
search(board, word.substr(), path, flag);
if(flag==true)
return;
path.pop_back();
}
if((lastpos+width)<(board.size()*width) && board[(lastpos+width)/width][(lastpos+width)%width]==c && find(path.begin(), path.end(), (lastpos+width))==path.end())
{
path.push_back(lastpos+width);
search(board, word.substr(), path, flag);
if(flag==true)
return;
path.pop_back();
}
if(lastpos%width> && board[(lastpos-)/width][(lastpos-)%width]==c && find(path.begin(), path.end(), (lastpos-))==path.end())
{
path.push_back(lastpos-);
search(board, word.substr(), path, flag);
if(flag==true)
return;
path.pop_back();
}
if((lastpos%width)<(width-) && board[(lastpos+)/width][(lastpos+)%width]==c && find(path.begin(), path.end(), (lastpos+))==path.end())
{
path.push_back(lastpos+);
search(board, word.substr(), path, flag);
if(flag==true)
return;
path.pop_back();
} } } class Solution {
public:
bool exist(vector<vector<char> > &board, string word) {
bool flag = false;
vector<int> path; search(board, word, path, flag); return flag;
}
};

[LeetCode OJ] Word Search 深度优先搜索DFS的更多相关文章

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

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

  3. LeetCode 79. Word Search单词搜索 (C++)

    题目: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fr ...

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

  6. 【算法入门】深度优先搜索(DFS)

    深度优先搜索(DFS) [算法入门] 1.前言深度优先搜索(缩写DFS)有点类似广度优先搜索,也是对一个连通图进行遍历的算法.它的思想是从一个顶点V0开始,沿着一条路一直走到底,如果发现不能到达目标解 ...

  7. 深度优先搜索DFS和广度优先搜索BFS简单解析(新手向)

    深度优先搜索DFS和广度优先搜索BFS简单解析 与树的遍历类似,图的遍历要求从某一点出发,每个点仅被访问一次,这个过程就是图的遍历.图的遍历常用的有深度优先搜索和广度优先搜索,这两者对于有向图和无向图 ...

  8. 利用广度优先搜索(BFS)与深度优先搜索(DFS)实现岛屿个数的问题(java)

    需要说明一点,要成功运行本贴代码,需要重新复制我第一篇随笔<简单的循环队列>代码(版本有更新). 进入今天的主题. 今天这篇文章主要探讨广度优先搜索(BFS)结合队列和深度优先搜索(DFS ...

  9. 深度优先搜索DFS和广度优先搜索BFS简单解析

    转自:https://www.cnblogs.com/FZfangzheng/p/8529132.html 深度优先搜索DFS和广度优先搜索BFS简单解析 与树的遍历类似,图的遍历要求从某一点出发,每 ...

随机推荐

  1. java开发功能代码汇总

    多文件上传 http://smotive.iteye.com/blog/1903606 java 常用代码 Struts2 前后台(Action ,jsp)传值.取值 Action public Li ...

  2. 在Mac下如何开Wifi

    1. 首先打开系统偏好设置,选择共享 2. 把互联网共享给勾上 在里面选择共享来源为以太网,共享方式为Wifi 3. 点开Wifi选项,在里面设置密码(如果需要设置密码的话) 4. 然后看状态栏上的W ...

  3. GPU 的硬件基本概念,Cuda和Opencl名词关系对应

    GPU 的硬件基本概念 Nvidia的版本: 实际上在 nVidia 的 GPU 里,最基本的处理单元是所谓的 SP(Streaming Processor),而一颗 nVidia 的 GPU 里,会 ...

  4. Tornado源码探寻(准备阶段)

    上一篇从一个简单的例子大致了解到Tornado框架的一个概述,同时也看清了web框架的本质. 接下来,我们从tornado程序的起始来分析其源码: 一.概述 上图是摘自朋友的博客里的内容,这张图很明确 ...

  5. (转)解决png图片在IE6下不透明的方法

    来源于:http://xzl52199.blog.163.com/blog/static/95206446201142174540220/ 一.传统的JavaScript方法 思路: 1.一个专门解决 ...

  6. 23讲 URL

    这是看完23讲后的小笔记,关于URL规则.伪静态. 一.URL规则 2.此处的区分大小写,也只是对第一个字母区分,并非对整个模块名. 3.模块名复杂时,且区分大小写,此时在地址栏访问时要用" ...

  7. jquerymobile知识点:button与a

    首先先上一段代码: <a href="#" data-role="button" onclick="change();" id=&qu ...

  8. 知名IT企业待遇一览表

    115家IT公司待遇一览表       作者是西电通院2013届毕业硕士,依据今年找工作的情况以及身边同学的汇总,总结各大公司的待遇例如以下,吐血奉献,公司比較全.下面绝对是各大公司2013届校招的数 ...

  9. mysql加入�管理员

    1.首先用超级管理员登录,然后点击权限button 2.点击加入�新用户,填写登录名和password,全局权限不用选,点击新建用户button 3.编辑新加入�的用户(编辑权限) 4.找到" ...

  10. android 判断应用程序是否已安装

    1.判断是否安装/** check the app is installed*/private boolean isAppInstalled(Context context,String packag ...