标签:mat pac length 路径 size pre thml 位置 tar
public class Solution { public boolean hasPath(char[] matrix, int rows, int cols, char[] str) { boolean[] flag=new boolean[matrix.length]; // flag数组默认初始化为falsefor(int i =0;i < rows;i ++){ for(int j=0;j < cols; j ++){ if(judge(matrix,flag,rows,cols,i,j,str,0)){ return true; } } } return false; } private boolean judge(char[] matrix,boolean[] flag, int rows, int cols, int i, int j, char[] str, int k) { int index=cols*i + j; if(i <0 ||j<0||i >= rows || j >= cols || matrix[index] != str[k] || flag[index] == true){ return false; } if(k == str.length-1) return true; flag[index] = true; if(judge(matrix,flag,rows,cols,i-1,j,str,k+1)|| judge(matrix,flag,rows,cols,i+1,j,str,k+1) || judge(matrix,flag,rows,cols,i,j-1,str,k+1) || judge(matrix,flag,rows,cols,i,j+1,str,k+1)){ return true; } flag[index] = false; return false; } }
标签:mat pac length 路径 size pre thml 位置 tar
原文地址:https://www.cnblogs.com/nlw-blog/p/12483719.html