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 ...
随机推荐
- linux 软件 手动添加至桌面或启动栏
1.创建软连接(也可以不用创建软连接,直接写绝对路径) sudo ln -s /opt/eclipse/eclipse /usr/bin/eclipse 2.创建desktop文件 sudo gedi ...
- CPU中的主要的寄存器
寄存器 名为寄存器的存储电路. 8种16位寄存器 AX accumulator 累加寄存器 CX counter 计数寄存器 DX data 数据寄存器 BX base 基址寄存器 SP stack ...
- 深入浅出 Java Concurrency (39): 并发总结 part 3 常见的并发陷阱
常见的并发陷阱 volatile volatile只能强调数据的可见性,并不能保证原子操作和线程安全,因此volatile不是万能的.参考指令重排序 volatile最常见于下面两种场景. a. 循环 ...
- VS2010-MFC(对话框:非模态对话框的创建及显示)
转自:http://www.jizhuomi.com/software/162.html 前面已经说过,非模态对话框显示后,程序其他窗口仍能正常运行,可以响应用户输入,还可以相互切换.本节会将上一讲中 ...
- linux命令快速手记 — 让手指跟上思考的速度(四)
pm2 list:列出pm2方式启动的所有程序 pm2 monit:显示每个应用程序的CPU和内存占用情况 scp:远程复制和本地上传,适用于本地ssh登录到远程服务器 scp root@10.10. ...
- Python-pip更改国内源
windows方式: 1.打开任意文件夹,在上方地址栏中输入%appdata% 2.在此目录里新建文件夹pip 3.在pip文件夹里新建文件名:pip.ini 4.把以下内容复制到pip.ini中,保 ...
- https://blog.csdn.net/qq_33169863/article/details/82977791
https://blog.csdn.net/qq_33169863/article/details/82977791 ** 查看设备连接 adb devices ** 列出手机已安装的包名 adb ...
- js中控制流管理的四种方法
引自http://es6.ruanyifeng.com/#docs/generator#yield--表达式 1.常用的回调方法 step1(function (value1) { step2(val ...
- 2019-6-14-WPF-shows-that-some-windows-in-multithreading-will-be-locked-in-the-PenThreadWorker-constr...
title author date CreateTime categories WPF shows that some windows in multithreading will be locked ...
- 网络流Sap算法
GDKOi就快要开始了.没时间打解析,直接上模板. #include <cstdio> #include <cstring> #include <algorithm> ...