剑指 Offer 12. 矩阵中的路径
请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一格开始,每一步可以在矩阵中向左、右、上、下移动一格。如果一条路径经过了矩阵的某一格,那么该路径不能再次进入该格子。例如,在下面的3×4的矩阵中包含一条字符串“bfce”的路径(路径中的字母用加粗标出)。
[[“a”,“b”,“c”,“e”],
[“s”,“f”,“c”,“s”],
[“a”,“d”,“e”,“e”]]
但矩阵中不包含字符串“abfb”的路径,因为字符串的第一个字符b占据了矩阵中的第一行第二个格子之后,路径不能再次进入这个格子。
示例 1:
输入:board = [[“A”,“B”,“C”,“E”],[“S”,“F”,“C”,“S”],[“A”,“D”,“E”,“E”]], word = “ABCCED”
输出:true
示例 2:
输入:board = [[“a”,“b”],[“c”,“d”]], word = “abcd”
输出:false
题解:
采用一个布尔数组来判断是否走过,先用俩for循环进行遍历,找到第一个符合要求的,然后在那个相同位置的布尔数组标记为true,然后再向上下左右进行查找寻找是否有符合要求的字符,有就再递归,当字符串遍历完了,也就返回true,就结束了
class Solution {
//这种思想比较好理解
public boolean exist(char[][] board, String word) {
if (board == null || board[0] == null || board.length == 0 || board[0].length == 0 || word == null || word.equals("")) {
return false;
}
boolean[][] isVisited = new boolean[board.length][board[0].length];
char[] chs = word.toCharArray();
//对数组进行遍历
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
//找到第一个符合的
if (board[i][j] == chs[0]) {
if (bfs(board, i, j, isVisited, chs, 0)) return true;
}
}
}
return false;
}
private boolean bfs(char[][] board, int i, int j, boolean[][] isVisited, char[] chs, int index) {
//这里是指遍历到最后一个了
if (index == chs.length) {
return true;
}
//也用来判断是不是之前走过的
if (i < 0 || j < 0 || i == board.length || j == board[0].length || isVisited[i][j] || board[i][j] != chs[index]) {
return false;
}
//当前位置是有效的
isVisited[i][j] = true;
//对上下左右进行遍历,如果有满足的就布尔数组的值就会变为true,
boolean res = bfs(board, i + 1, j, isVisited, chs, index + 1)
|| bfs(board, i - 1, j, isVisited, chs, index + 1)
|| bfs(board, i, j + 1, isVisited, chs, index + 1)
|| bfs(board, i, j - 1, isVisited, chs, index + 1);
isVisited[i][j] = false;
return res;
}
}