import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param matrix char字符型二维数组 * @param word string字符串 * @return bool布尔型 */ public boolean hasPath (char[][] matrix, String word) { // write code here if(matrix == null || matrix.length == 0 || matrix[0].length == 0 || word.length() == 0){ return false; } for(int i = 0; i < matrix.length; ++i){ for(int j = 0; j < matrix[0].length; ++j){ if(dfs(matrix, word, i, j, 0)){ return true; } } } return false; } public boolean dfs(char[][] matrix, String word, int i, int j, int k){ if(i < 0 || i > matrix.length - 1 || j < 0 || j > matrix[0].length - 1 || matrix[i][j] != word.charAt(k)){ return false; } if(k == word.length() - 1 && word.charAt(k) == matrix[i][j]){ return true; } matrix[i][j] = '\0'; boolean res = dfs(matrix, word, i + 1, j, k + 1) || dfs(matrix, word, i - 1, j, k + 1) || dfs(matrix, word, i, j + 1, k + 1) || dfs(matrix, word, i, j - 1, k + 1); matrix[i][j] = word.charAt(k); return res; } }