当前位置:网站首页>剑指 Offer 12. 矩阵中的路径
剑指 Offer 12. 矩阵中的路径
2022-07-03 12:10:00 【嗝~~~~】
剑指 Offer 12. 矩阵中的路径
难度中等601收藏分享切换为英文接收动态反馈
给定一个 m x n 二维字符网格 board 和一个字符串单词 word 。如果 word 存在于网格中,返回 true ;否则,返回 false 。
单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。
例如,在下面的 3×4 的矩阵中包含单词 “ABCCED”(单词中的字母已标出)。
示例 1:
输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"输出:true
示例 2:
输入:board = [["a","b"],["c","d"]], word = "abcd"输出:false
提示:
- 1 <= board.length <= 200
- 1 <= board[i].length <= 200
- board 和 word 仅由大小写英文字母组成
*思路:
模仿下述思路,每次理清楚。
思路

代码
class Solution {
public:
bool flag=0;
bool dfs(vector<vector<char> >& board, string word,int i,int j,int k){
if(i<0||j<0||i>=board.size()||j>=board[0].size()||board[i][j]!=word[k])
return 0;
if(k==word.size()-1)
return 1;
//向四个方向
board[i][j]='\0';
//剪枝,不用+而是||
bool res=dfs(board,word,i-1,j,k+1)|| dfs(board,word,i+1,j,k+1) || dfs(board,word,i,j-1,k+1) || dfs(board,word,i,j+1,k+1);
board[i][j]=word[k];
return res;
}
bool exist(vector<vector<char> >& board, string word) {
if(word=="") return true;
for(int i=0;i<board.size();i++){
for(int j=0;j<board[0].size();j++){
if(dfs(board,word,i,j,0))
return 1;
}
}
return 0;
}
};
边栏推荐
- Nodejs+express+mysql realizes login function (including verification code)
- ImportError: No module named examples. tutorials. mnist
- The best shortcut is no shortcut
- Huffman coding experiment report
- 01 three solutions to knapsack problem (greedy dynamic programming branch gauge)
- Low code platform international multilingual (I18N) technical solution
- SLF4J 日志门面
- Differences and connections between final and static
- Bert running error: attributeerror: module'tensorflow contrib. tpu' has no attribute 'InputPipelineConfig'
- CVPR 2022 图像恢复论文
猜你喜欢

Leetcode234 palindrome linked list

【ArcGIS自定义脚本工具】矢量文件生成扩大矩形面要素

4. 无线体内纳米网:电磁传播模型和传感器部署要点

Idea packages the web project into a war package and deploys it to the server to run

initial、inherit、unset、revert和all的区别
![[problem exploration and solution of one or more filters or listeners failing to start]](/img/82/e7730d289c4c1c4800b520c58d975a.jpg)
[problem exploration and solution of one or more filters or listeners failing to start]

并网-低电压穿越与孤岛并存分析

Node.js: express + MySQL的使用

ncnn神经网络计算框架在香橙派OrangePi 3 LTS开发板中的使用介绍

强大的头像制作神器微信小程序
随机推荐
Attack and defense world mobile--ph0en1x-100
Gan totem column bridgeless boost PFC (single phase) seven PFC duty cycle feedforward
Kotlin - 改良装饰者模式
[exercice 7] [principe de la base de données]
Tianyi ty1208-z brush machine detailed tutorial (free to remove)
Using swift language features, write a pseudo-random number generator casually
The solution to change the USB flash disk into a space of only 2m
Cache penetration and bloom filter
Sqoop1.4.4原生增量导入特性探秘
Use Tencent cloud IOT platform to connect custom esp8266 IOT devices (realized by Tencent continuous control switch)
ImportError: No module named examples. tutorials. mnist
Analysis of a music player Login Protocol
剑指 Offer 11. 旋转数组的最小数字
Xctf mobile--rememberother problem solving
studio All flavors must now belong to a named flavor dimension. Learn more
OpenStack节点地址改变
Swift Error Handling
C graphical tutorial (Fourth Edition)_ Chapter 13 entrustment: delegatesamplep245
【数据挖掘复习题】
[review questions of database principles]