Leetcode79. Word Search单词搜索
给定一个二维网格和一个单词,找出该单词是否存在于网格中。
单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。
示例:
board = [ ['A','B','C','E'], ['S','F','C','S'], ['A','D','E','E'] ] 给定 word = "ABCCED", 返回 true. 给定 word = "SEE", 返回 true. 给定 word = "ABCB", 返回 false.
class Solution {
public:
vector<vector<int> > visit;
int len;
int r;
int c;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
bool exist(vector<vector<char> >& board, string word)
{
len = word.size();
if(len == 0)
return true;
r = board.size();
if(r == 0)
return false;
c = board[0].size();
if(r * c < len)
return false;
for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j++)
{
visit.clear();
visit = vector<vector<int> >(r, vector<int>(c, 0));
if(board[i][j] == word[0])
{
visit[i][j] = 1;
if(DFS(board, 1, word, i, j))
return true;
}
}
}
return false;
}
bool DFS(vector<vector<char> >& board, int pos, string cmp, int x, int y)
{
if(pos >= cmp.size())
return true;
for(int i = 0; i < 4; i++)
{
int xx = x + dx[i];
int yy = y + dy[i];
if(xx < 0 || xx >= r || yy < 0 || yy >= c)
continue;
if(visit[xx][yy] == 1)
continue;
if(board[xx][yy] != cmp[pos])
continue;
visit[xx][yy] = 1;
if(DFS(board, pos + 1, cmp, xx, yy))
return true;
visit[xx][yy] = 0;
}
return false;
}
};
Leetcode79. Word Search单词搜索的更多相关文章
- [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单词搜索 (C++)
题目: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fr ...
- 079 Word Search 单词搜索
给定一个二维面板和一个单词,找出该单词是否存在于网格中.这个词可由顺序相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字母不允许被重复使用.例如,给定 二 ...
- [LeetCode] 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 词语搜索
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- [Leetcode] 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 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 ...
- Leetcode79 Word Search
题目描述 Given a 2D board and a word, find if the word exists in the grid. The word can be constructed f ...
- [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 ...
随机推荐
- 箭头函数报错:Add @babel/plugin-proposal-class-properties (https://git.io/vb4SL) to the 'plugins' section of your Babel config to enable transformation.
解决:根目录新建babel.config.js加入如下内容 module.exports = { presets: [ "@babel/preset-env", "@ba ...
- TCP/IP四层模型和OSI七层模型(模型分层的作用是什么)
TCP/IP四层模型和OSI七层模型的概念(模型分层的作用是什么) 一.总结 一句话总结: 减轻问题的复杂程度,一旦网络发生故障,可迅速定位故障所处层次,便于查找和纠错: 在各层分别定义标准接口,使具 ...
- OpenCASCADE点向圆柱面投影
OpenCASCADE点向圆柱面投影 eryar@163.com OpenCASCADE的类Extrema_ExtPElS提供了点到基本曲面的投影计算功能,距离可能是最大值或是最小值.如下图所示的点到 ...
- Python学习day38-并发编程(线程)
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
- 10张图带你深入理解Docker容器和镜像-转
转载:http://dockone.io/article/783 这篇文章希望能够帮助读者深入理解Docker的命令,还有容器(container)和镜像(image)之间的区别,并深入探讨容器和运行 ...
- 08-2-if的其他写法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- ES6之主要知识点(三)字符串
引自:http://es6.ruanyifeng.com/#docs/string#codePointAt codePointAt() String.fromCodePoint() at() incl ...
- Activiti 接收任务活动
流程中往往需要特定人接受任务并进行一定操作才能继续进行下去. 代码如下 import java.io.InputStream; import org.activiti.engine.ProcessEn ...
- 第一次个人项目【词频统计】——测试样例分析&性能分析
[空文件测试](认为空文件行数为0) [基本测试] [大小写测试] 可以看出abcd同类型的词出现了三次,而单词树中存储的是ABCd,满足大小写要求. [复杂文件测试] 前三项数据比较接近,但是单词和 ...
- Store工作原理