当前位置:网站首页>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;
}
};
边栏推荐
- Step num problem
- Embedded development: embedded foundation -- threads and tasks
- Rand1 generates rand9
- Arrow parquet
- Log in to Baidu online disk with cookies (websites use cookies)
- The database empties the table data and makes the primary key start from 1
- 增加 swap 空间
- 什么是聚类分析?聚类分析方法的类别[通俗易懂]
- Recommended books | essentials of industrial digital transformation: methods and Practice
- [today in history] July 18: Intel was founded; The first photo was posted on the world wide web; EBay spins off PayPal
猜你喜欢

Key network protocols in tcp/ip four layer model

Kubernetes进阶部分学习笔记

Socket error Event: 32 Error: 10053. Connection closing...Socket close

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

Clickhouse notes 02 -- installation test clickvisual

Advantages of network virtualization of various manufacturers
![[advanced mathematics] [5] definite integral and its application](/img/b2/62748b7533982f2b864148e0857490.png)
[advanced mathematics] [5] definite integral and its application
![[matlab] download originality documents based on oil monkey script and MATLAB](/img/c2/1788b758778ba73dd02fb0d006869e.png)
[matlab] download originality documents based on oil monkey script and MATLAB

移动web布局方法

【ONNX】pytorch模型导出成ONNX格式:支持多参数与动态输入
随机推荐
CarSim simulation quick start (XIV) - CarSim Simulink joint simulation
【高等数学】【3】微分中值定理与导数的应用
Learn FPGA from the bottom structure (16) -- customization and testing of pll/mmcm IP
2022.7.24-----leetcode.1184
Embedded development: embedded foundation -- threads and tasks
移动web布局方法
DIY personal server (DIY storage server)
股票软件开发
Introduction to several scenarios involving programming operation of Excel in SAP implementation project
Online random coin tossing positive and negative statistical tool
How to choose a microservice registration center?
[advanced mathematics] [4] indefinite integral
[advanced mathematics] [3] Application of differential mean value theorem and derivative
【TensorRT】trtexec工具转engine
结构体,枚举类型与联合体
Do you still have certificates to participate in the open source community?
JS作用域与作用域链
QQ是32位还是64位软件(在哪看电脑是32位还是64位)
参与开源社区还有证书拿?
增加 swap 空间