当前位置:网站首页>leetcode-79:单词搜索
leetcode-79:单词搜索
2022-07-25 20:36:00 【菊头蝙蝠】
题目
题目连接
给定一个 m x n 二维字符网格 board 和一个字符串单词 word 。如果 word 存在于网格中,返回 true ;否则,返回 false 。
单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。
示例 1:
输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
输出:true
示例 2:

输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
输出:true
示例 3:
输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
输出:false

解题
方法一:回溯
class Solution {
public:
vector<vector<int>> dirs={
{
1,0},{
-1,0},{
0,1},{
0,-1}};
vector<vector<bool>> visit;
bool dfs(vector<vector<char>>& board,string& word,int index,int x,int y){
if(index==word.size()) return true;
if(x<0||x>=board.size()||y<0||y>=board[0].size()) return false;
if(visit[x][y]) return false;
char c=word[index];
if(board[x][y]!=c) return false;
for(vector<int>& dir:dirs){
int nx=x+dir[0];
int ny=y+dir[1];
visit[x][y]=true;
if(dfs(board,word,index+1,nx,ny)) return true;
visit[x][y]=false;
}
return false;
}
bool exist(vector<vector<char>>& board, string word) {
for(int i=0;i<board.size();i++){
for(int j=0;j<board[0].size();j++){
visit=vector<vector<bool>>(board.size(),vector<bool>(board[0].size(),false));
if(dfs(board,word,0,i,j)) return true;
}
}
return false;
}
};
边栏推荐
- Behind every piece of information you collect, you can't live without TA
- Jmeter——接口测试
- [onnx] export pytorch model to onnx format: support multi parameter and dynamic input
- [matlab] download originality documents based on oil monkey script and MATLAB
- Difference Between Accuracy and Precision
- Open source SPL enhances mangodb computing
- 网络RTK无人机上机测试[通俗易懂]
- FanoutExchange交换机代码教程
- 【高等数学】【4】不定积分
- Web crawler principle analysis "suggestions collection"
猜你喜欢

Advantages of network virtualization of various manufacturers

Online random coin tossing positive and negative statistical tool
![[today in history] July 3: ergonomic standards act; The birth of pioneers in the field of consumer electronics; Ubisoft releases uplay](/img/18/b06e2e5a2f76dc2da1c2374b8424b3.png)
[today in history] July 3: ergonomic standards act; The birth of pioneers in the field of consumer electronics; Ubisoft releases uplay
![[tensorrt] dynamic batch reasoning](/img/59/42ed0074de7162887bfe2c81891e20.png)
[tensorrt] dynamic batch reasoning

【高等数学】【3】微分中值定理与导数的应用

Detailed explanation of document operation
![[advanced mathematics] [3] Application of differential mean value theorem and derivative](/img/a9/3b024dbbb201bee4eed6c9f6ce3001.png)
[advanced mathematics] [3] Application of differential mean value theorem and derivative

Solution to oom exceptions caused by improper use of multithreading in production environment (supreme Collection Edition)

数据库清空表数据并让主键从1开始

【单细胞高级绘图】07.KEGG富集结果展示
随机推荐
飞行器pid控制(旋翼飞控)
How to use buffer queue to realize high concurrent order business (glory Collection Edition)
Clickhouse notes 02 -- installation test clickvisual
【高等数学】【3】微分中值定理与导数的应用
Proxy implements MySQL read / write separation
结构体,枚举类型与联合体
Today's sleep quality record 75 points
[advanced mathematics] [3] Application of differential mean value theorem and derivative
Automated testing ----- selenium (I)
During the interview, I was asked how to remove the weight of MySQL, and who else wouldn't?
【高等数学】【8】微分方程
Go language go language built-in container
String of sword finger offer question bank summary (II) (C language version)
[tensorrt] dynamic batch reasoning
JMeter - interface test
网络爬虫原理解析「建议收藏」
Online random coin tossing positive and negative statistical tool
TGA file format (waveform sound file format)
每条你收藏的资讯背后,都离不开TA
“链”接无限可能:数字资产链,精彩马上来!