思路:
public class Solution { //注意迭代!
public boolean hasPath(char[] matrix, int rows, int cols, char[] str)
{
if(matrix==null||rows<=0||cols<=0||str==null)
return false;
if(str.length==0)
return true;
boolean[] visited=new boolean[rows*cols];
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++){
if(isHasPath(matrix,i,j,rows,cols,0,visited,str))
return true;
}
}
return false;
}
private boolean isHasPath(char[] matrix,int row,int col,int rows,int cols,int k,boolean[] visited,char[] str){
if(row<0 || row>=rows || col<0 || col>=cols || str[k]!=matrix[row*cols+col] || visited[row*cols+col]){
return false;
}
if(k==str.length-1){
return true;
}
visited[row*cols+col]=true; //表示访问过
if(isHasPath(matrix, row+1, col, rows, cols, k+1, visited, str) ||
isHasPath(matrix, row, col+1, rows, cols, k+1, visited, str) ||
isHasPath(matrix, row-1, col, rows, cols, k+1, visited, str) ||
isHasPath(matrix, row, col-1, rows, cols, k+1, visited, str)){
return true;
}
visited[row*cols+col]=false;
return false;
}
}