版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jun8148/article/details/85515274
13. 矩阵中的路径
问题描述:
请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则该路径不能再进入该格子。 例如 a b c e s f c s a d e e 矩阵中包含一条字符串”bcced”的路径,但是矩阵中不包含”abcb”路径,因为字符串的第一个字符b占据了矩阵中的第一行第二个格子之后,路径不能再次进入该格子。
分析:
这是一个典型的回溯法问题。
- 任取矩阵中的一个格子,作为起点,如果路径上的第i 个字符不是ch,那么这格子不可能处于路径上的第i 个位置。如果路径上的第i 个字符正好是ch,那么往相邻的格子寻找路径上的第i+1个字符。除在矩阵边界上的格子之外,其他格子都有4个相邻的格子。重复这个过程直到路径上的所有字符都在矩阵中找到相应的位置。
- 由于回朔法的递归特性,路径可以被开成一个栈。当在矩阵中定位了路径中前n个字符的位置之后,在与第n个字符对应的格子的周围都没有找到第n+1个字符,这个时候只要在路径上回到第n-1个字符,重新定位第n个字符。
- 由于路径不能重复进入矩阵的格子,还需要定义和字符矩阵大小一样的布尔值矩阵,用来标识路径是否已经进入每个格子。 当矩阵中坐标为(row,col)的格子和路径字符串中相应的字符一样时,从4个相邻的格子(row,col-1),(row-1,col),(row,col+1)以及(row+1,col)中去定位路径字符串中下一个字符
- 如果4个相邻的格子都没有匹配字符串中下一个的字符,表明当前路径字符串中字符在矩阵中的定位不正确,我们需要回到前一个,然后重新定位。
- 一直重复这个过程,直到路径字符串上所有字符都在矩阵中找到合适的位置
代码:
/**
* Class day14 ...
*
* @author LiJun
* Created on 2019/1/1
*/
// 矩阵中的路径
public class day14 {
public static boolean hasPath(char[] matrix, int rows, int cols, char[] str) {
if (matrix == null || rows < 1 || cols < 1 || str == null) {
return false;
}
boolean[] visited = new boolean[rows * cols];
/*// java中 boolean的默认初值是false 所以可有可无
for(int i = 0; i < rows*cols; i++){
visited[i] = false;
}
*/
int pathLength = 0;
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
if (hasPathCore(matrix, rows, cols, row, col, str, pathLength, visited)) {
return true;
}
}
}
return false;
}
private static boolean hasPathCore(char[] matrix, int rows, int cols,
int row, int col, char[] str, int pathLength, boolean[] visited) {
if (pathLength == str.length) {
return true;
}
boolean hasPath = false;
// 保证数据的有效性 且没被访问过
if (row >= 0 && row < rows && col >= 0 && col < cols
&& matrix[row * cols + col] == str[pathLength] &&
!visited[row * cols + col]) {
pathLength++;
visited[row * cols + col] = true;
hasPath = hasPathCore(matrix, rows, cols, row, col - 1, str, pathLength, visited)
|| hasPathCore(matrix, rows, cols, row - 1, col, str, pathLength, visited)
|| hasPathCore(matrix, rows, cols, row, col + 1, str, pathLength, visited)
|| hasPathCore(matrix, rows, cols, row + 1, col, str, pathLength, visited);
if (!hasPath) {
--pathLength;
visited[row * cols + col] = false;
}
}
return hasPath;
}
public static void main(String[] args) {
String s = "abtgcfcsjdeh";
char[] matrix = s.toCharArray();
char[] str = {'a','b','f','b'};
System.out.println(hasPath(matrix, 3, 4, str));
str = new char[]{'b','f','c','e'};
System.out.println(hasPath(matrix, 3, 4, str));
}
}