Please design a function to determine whether there is a path that contains all the characters of a string in a matrix. The path can start from any grid in the matrix, and each step can move one grid to the left, right, up, and down in the matrix. If a path passes through a grid of the matrix, then the path cannot enter the grid again. For example, the following 3×4 matrix contains a path for the character string "bfce" (the letters in the path are marked in bold).
[[“a”,“b”,“c”,“e”],
[“s”,“f”,“c”,“s”],
[“a”,“d”,“e”,“e”]]
But the matrix does not contain the path of the string "abfb", because after the first character b of the string occupies the second grid in the first row of the matrix, the path cannot enter this grid again.
Example 1:
Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D", "E","E"]], word = "ABCCED"
Output: true
Example 2:
Input: board = [["a","b"],["c","d"]], word = "abcd"
Output: false
Source: LeetCode
Link: Click to jump to https://leetcode-cn.com/problems/ju-zhen-zhong-de-lu-jing-lcof
//回溯法,又称深度优先搜索(DFS)
class Solution {
//函数返回是否存在该路径
public boolean exist(char[][] board, String word) {
char[] ch = word.toCharArray();
//判断位置是否被走过,默认false:没走过
boolean[][] isused = new boolean[board.length][board[0].length];
//循环遍历二维数组每个位置
for(int i = 0; i < board.length; i++) {
for(int j = 0; j < board[0].length; j++) {
//然后每个位置判断是否可以达成word路径
if(dfs(board, ch, isused, i, j, 0)) return true;
}
}
return false;
}
//递归每个位置,回溯法:先朝一个方向搜到底,再回溯至上个节点,沿另一个方向搜索,以此类推
public boolean dfs(char[][] board, char[] ch, boolean[][] isused, int i, int j, int index) {
//不符合word路径的条件
//1.该位置越界
//2.board(i,j)位置不符合word(index)位置
//3.该位置已经走过
if(i < 0 || j < 0 || i >= board.length || j >= board[0].length || board[i][j] != ch[index] || isused[i][j]) {
return false;
}
//符合word路径的条件:
if(index == ch.length-1) {
return true;
}
//已经走过这个位置了
isused[i][j] = true;
//step先向上一直搜索,不行了再回溯上一个节点,再向下。。。再向左。。。再向右。。。
boolean step = false;
step = dfs(board, ch, isused, i-1, j, index+1) ||
dfs(board, ch, isused, i+1, j, index+1) ||
dfs(board, ch, isused, i, j-1, index+1) ||
dfs(board, ch, isused, i, j+1, index+1);
//因为走不下去,退回到上一位置了,所以该位置重置为false:没走过
isused[i][j] = false;
return step;
}
}