public class Solution {
boolean flag
=false;
int xx
[]={-1,1,0,0};
int yy
[]={0,0,1,-1};
boolean vis
[][];
public boolean hasPath(char[] matrix
, int rows
, int cols
, char[] str
)
{
vis
=new boolean[rows
][cols
];
int l
=str
.length
;
for(int i
=0;i
<rows
;i
++){
for(int j
=0;j
<cols
;j
++){
if(matrix
[i
*cols
+j
]==str
[0]){
dfs(matrix
,i
,j
,rows
,cols
,str
,0,l
);
}
}
}
return flag
;
}
void dfs(char []matrix
,int x
,int y
,int rows
,int cols
,char[] str
,int k
,int l
){
if(matrix
[x
*cols
+y
]==str
[k
]){
vis
[x
][y
]=true;
k
++;
if(k
==l
){
flag
=true;
return;
}
for(int i
=0;i
<4;i
++){
int x1
=x
+xx
[i
];
int y1
=y
+yy
[i
];
if(x1
>=0&&x1
<rows
&&y1
>=0&&y1
<cols
&&vis
[x1
][y1
]==false){
dfs(matrix
,x1
,y1
,rows
,cols
,str
,k
,l
);
}
}
vis
[x
][y
]=false;
}
}
}
转载请注明原文地址: https://lol.8miu.com/read-9684.html