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. SWFUpload无刷新文件批量上传

    一.首先将SWFUpload所有文件加入项目中,如图

  2. 让你的Git水平更上一层楼的10个小贴士

    注意:本文中,一些命令包含含有方括号的部分(e.g.git add -p [file_name]).在这些例子中,您要在该处插入所需的数字,标示符等.而不需要保留方括号. 1.Git自动补全 如果你在 ...

  3. 4 weekend110的hdfs&mapreduce测试 + hdfs的实现机制初始 + hdfs的shell操作 + 无密登陆配置

    Hdfs是根/目录,windows是每一个盘符, 1  从Linux里传一个到,hdfs里去 2  从hdfs里下一个到,linux里去 想从hdfs里,下载到linux, 涨知识,记住,hdfs是建 ...

  4. CentOS搭建GIT服务器【一】-仓库搭建以及基于gitosis的SSH方式访问

    1.安装GIT核心 yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel gcc g ...

  5. Android学习(一)

    #常见布局 ###线性布局 有一个布局方向,水平或者竖直 在竖直布局下,左对齐.右对齐,水平居中生效 在水平布局下,顶部对齐.底部对齐.竖直居中生效 权重:按比例分配屏幕的剩余宽度或者高度 ###相对 ...

  6. 英文Ubantu系统安装中文输入法

    以前都是安装的中文Ubantu,但是有时候用命令行的时候中文识别不好,会出现错误,所以这次安装了英文版,但是安装后发现输入法不好用,于是就要自己安装输入法. 安装环境为Ubantu13.04 1.卸载 ...

  7. MVC入门教程-视图中的Layout使用

    本文目标 1.能够重用Razor模板进行页面的组件化搭建 本文目录 1.母板页_Layout.cshtml 2.用户自定义控件 3.默认Layout引用的使用(_ViewStart.cshtml) 1 ...

  8. Android MediaPlayer Error/Info Code

    1. 常见错误 error(-38, 0) 我觉得-38表示在当前的MediaPlayer状态下,不能运行你的操作. 详细怎样做请參考:Android MediaPlayer 另外我在其它资料中.发现 ...

  9. android81 多线程下载和断电续传

    package com.itheima.multithreaddownload; import java.io.BufferedReader; import java.io.File; import ...

  10. java数组 数组工具类Arrays

    一.数组 1.java有严格的数据类型限制,一个数组只能声明一个数据类型,存放同一种数据类型. 2.虽然只能存放一种数据类型,假设A , 如果数据类型B 继承A,依然能存放进入数组. 3.数组的初始化 ...