回溯法:请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则之后不能再次进入这个格子。 例如 a b c e s f c s a d e e 这样的3 X 4 矩阵中包含一条字符串"bcced"的路径,但是矩阵中不包含"abcb"路径,因为字符串的第一个字符b占据了矩阵中的第一行第二个格子之后,路径不能再次进入该格子。
思路:回溯法:首先任意一个点都有可能成为起点,所以要获得任意一点的坐标(位于第几行,第几列)
- 其次要有一个数组记录这个点是否被访问过,同时要有一个指针来记录字符串中字符的位置。
- 当某个点成为合法的起点时,即这个点与字符串中第一个字符相等,则可以继续朝上下左右搜索下一个点;
- 如果这个点不能成为合法的起点,则恢复现场(这个点没有被访问过且字符串指针后退
public boolean hasPath(char[] matrix, int rows, int cols, char[] str){
boolean[] visited = new boolean[rows * cols];
// 创建只有一个元素的索引数组,用来表示当前str中索引
// 之所以用数组,是因为在传参过程中,值不变,因此更加方便
int[] index = {0};
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (isPath(matrix, rows, cols, i, j, str, visited, index)) {
return true;
}
}
}
return false;
}
public boolean isPath(char[] matrix, int rows, int cols, int row, int col, char[] str, boolean[] visited, int[] index) {
if (index[0] == str.length) {
return true;
}
boolean flag = false;
// 当前结点在matrix中索引位置为 row * cols + col
if (row >= 0 && row < rows && col >= 0 && col < cols &&
!visited[row * cols + col] && str[index[0]] == matrix[row * cols + col]) {
// 指针右移,并且将访问数组相应位置置为true
visited[row * cols + col] = true;
index[0]++;
// 回溯:进行上下左右搜索
flag = isPath(matrix, rows, cols, row - 1, col, str, visited, index) ||
isPath(matrix, rows, cols, row + 1, col, str, visited, index) ||
isPath(matrix, rows, cols, row, col - 1, str, visited, index) ||
isPath(matrix, rows, cols, row, col + 1, str, visited, index);
// 还原现场
if (!flag) {
index[0]--;
visited[row * cols + col] = false;
}
}
return flag;
}