当前位置:网站首页>牛客剑指offer--JZ12 矩阵中的路径
牛客剑指offer--JZ12 矩阵中的路径
2022-07-27 05:01:00 【叶辰 .】
JZ12 矩阵中的路径
请设计一个函数,用来判断在一个n乘m的矩阵中是否存在一条包含某长度为len的字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则该路径不能再进入该格子。 例如 。。。。 矩阵中包含一条字符串"bcced"的路径,但是矩阵中不包含"abcb"路径,因为字符串的第一个字符b占据了矩阵中的第一行第二个格子之后,路径不能再次进入该格子。
数据范围:0≤n,m≤20 0 \le n,m \le 20\ 0≤n,m≤20 ,1≤len≤25 1\le len \le 25\ 1≤len≤25
示例1:
输入:[[a,b,c,e],[s,f,c,s],[a,d,e,e]],“abcced”
返回值:true
示例2:
输入:[[a,b,c,e],[s,f,c,s],[a,d,e,e]],“abcb”
返回值:false
题目分析:
- dfs遍历:pb存入的是bool型的是否遍历数组矩阵a[][]当前元素,传入参数为矩阵数组、结果字符串、当前的坐标i和j,还有到达的层数index
- dfs需要步骤–截止条件、候选节点。
- 截止条件为已遍历到结果字符串的最后一位
- 候选节点为未越界的上下左右坐标,并且满足条件!pb[x][y]&&p[x][y]==str[index],就往下一层继续遍历
代码如下:
class Solution {
public:
bool pb[26][26]={false};
bool flag=false;
bool hasPath(vector<vector<char> >& p, string str) {
for(int i=0;i<p.size();i++){
for(int j=0;j<p[0].size();j++){
if(!pb[i][j]&&p[i][j]==str[0]){
pb[i][j]=true;
dfs(p,str,1,i,j);
pb[i][j]=false;
if(flag){
return true;
}
}
}
}
return false;
}
void dfs(vector<vector<char> >& p, string str,int index,int i,int j){
//截至条件
if(flag==true||index==str.size()){
flag=true;
return;
}
//候选节点
for(int x=i-1;x<=i+1;x++){
for(int y=j-1;y<=j+1;y++){
//筛选合法
if((x==i-1&&y==j)||(x==i+1&&y==j)||(x==i&&y==j-1)||(x==i&&y==j+1)){
if(x>=0&&y>=0&&x<p.size()&&y<p[0].size()){
//最终筛选
if(!pb[x][y]&&p[x][y]==str[index]){
// cout<<str[index]<<" "<<x<<" "<<y<<endl;
pb[x][y]=true;
dfs(p,str,index+1,x,y);
pb[x][y]=false;
}
}
}
}
}
}
};
边栏推荐
- Sub database and sub table
- Detailed description of polymorphism
- Introduction to dynamic memory functions (malloc free calloc realloc)
- C中文件I/O的使用
- feign调用丢失请求头问题解决及原理分析
- 微淼联合创始人孙延芳:以合规为第一要义,做财商教育“正规军”
- JVM上篇:内存与垃圾回收篇十四--垃圾回收器
- Mysql表的约束
- Could not autowire. No beans of ‘userMapper‘ type found.
- Final Cut Pro Chinese tutorial (1) basic understanding of Final Cut Pro
猜你喜欢

Raspberry pie RTMP streaming local camera image

Jmeter 界面如何汉化?

事件(event)

Use ngrok for intranet penetration

对话框简介

精选用户故事|洞态在聚水潭的误报率几乎为0,如何做到?

智慧展厅设计的优势及适用行业分析

《Robust and Precise Vehicle Localization based on Multi-sensor Fusionin Diverse City Scenes》翻译

How to import PS style? Photoshop style import tutorial

Read write separation and master-slave synchronization
随机推荐
File dialog box
标准对话框 QMessageBox
Raspberry pie output PWM wave to drive the steering gear
Web框架介绍
Detailed explanation of pointer constant and constant pointer
Installation and template setting of integrated development environment pychar
SSM framework integration
What about PS too laggy? A few steps to help you solve the problem
Transaction database and its four characteristics, principle, isolation level, dirty read, unreal read, non repeatable read?
深入 Qt5 信号槽新语法
Interface and abstract class / method learning demo
How does PS import LUT presets? Photoshop import LUT color preset tutorial
Sunyanfang, co-founder of WeiMiao: take compliance as the first essence and become the "regular army" of financial and business education
2、 MySQL advanced
Row, table, page, share, exclusive, pessimistic, optimistic, deadlock
How to copy Photoshop layers to other documents
【无标题】按照一定的条件,对 i 进行循环累加。条件通常为循环数组的长度,当超过长度就停止循环。因为对象无法判断长度,所以通常搭配 Object.keys() 使用。\nforEach 一般认为是 普
[error reporting] cannot read property 'parsecomponent' of undefined
SVN使用详解
JVM上篇:内存与垃圾回收篇九--运行时数据区-对象的实例化,内存布局与访问定位