参考Lc上大神的思路 多了个转换成二维矩阵的步骤 13ms 9728K
https://leetcode-cn.com/problems/ju-zhen-zhong-de-lu-jing-lcof/solution/mian-shi-ti-12-ju-zhen-zhong-de-lu-jing-shen-du-yo/
public class Solution { public boolean hasPath(char[] matrix, int rows, int cols, char[] str){ char[][] array = new char[rows][cols]; for (int i = 0; i < rows ; i++) { for(int j = 0; j < cols; j++) { array[i][j] = matrix[i*cols + j]; } } for(int i = 0; i < array.length; i++){ for(int j = 0; j< array[0].length; j++){ if(dfs(array, str, i, j, 0)) return true; } } return false; } public boolean dfs(char[][] array,char [] str, int i,int j,int k){ if(i >= array.length || i < 0 || j >= array[0].length || j < 0 || array[i][j] != str[k] ){ return false; } if(k == str.length - 1)return true; char temp = array[i][j]; array[i][j] = '/'; boolean res = dfs(array,str,i + 1, j, k + 1) || dfs(array,str,i - 1, j, k + 1) ||dfs(array,str,i, j + 1, k + 1) ||dfs(array,str,i, j - 1, k + 1); array[i][j]= temp; return res; } }