package com.example.lettcode.offer;
/**
* @Class Exist
* @Description 剑指 Offer 12. 矩阵中的路径
* 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。
* 路径可以从矩阵中的任意一格开始,每一步可以在矩阵中向左、右、上、下移动一格。
* 如果一条路径经过了矩阵的某一格,那么该路径不能再次进入该格子。
* 例如,在下面的3×4的矩阵中包含一条字符串“bfce”的路径(路径中的字母用加粗标出)。
* <p>
* [["a","b","c","e"],
* ["s","f","c","s"],
* ["a","d","e","e"]]
* 但矩阵中不包含字符串“abfb”的路径,因为字符串的第一个字符b占据了矩阵中的第一行第二个格子之后,
* 路径不能再次进入这个格子。
* <p>
* 示例 1:
* 输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
* 输出:true
* <p>
* 示例 2:
* 输入:board = [["a","b"],["c","d"]], word = "abcd"
* 输出:false
* 提示:
* <p>
* 1 <= board.length <= 200
* 1 <= board[i].length <= 200
* @Author
* @Date 2020/6/29
**/
public class Exist {
/**
* 解法1:利用dfs + 剪枝法
*/
public static boolean exist(char[][] board, String word) {
if (board == null || board.length == 0) return false;
char[] words = word.toCharArray();
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (dfs(board, words, i, j, 0)) return true;
}
}
return false;
}
private static boolean dfs(char[][] board, char[] words, int i, int j, int k) {
// 终止条件 越界或者全部匹配
// 其中k表示words中第k个匹配的字符
if (i >= board.length || i < 0
|| j >= board[0].length || j < 0
|| k>=words.length || board[i][j] != words[k])
return false;
// 表示words全部匹配
if(k == words.length-1) return true;
// 标记当前元素,防止重复搜索
char temp = board[i][j];
board[i][j] = '/';
// 上下左右四个方向
boolean flag = dfs(board, words, i + 1, j, k + 1) || dfs(board, words, i, j + 1, k + 1)
|| dfs(board, words, i - 1, j, k + 1) || dfs(board, words, i, j - 1, k + 1);
// 元素还原
board[i][j] = temp;
return flag;
}
public static void main(String[] args) {
char[][] board = new char[][]{
{'a', 'b', 'c', 'e'},
{'s', 'f', 'c', 's'},
{'a', 'd', 'e', 'e'}};
String word = "abcced";
boolean ans = exist(board, word);
System.out.println("demo01 result:" + ans);
board = new char[][]{{'a', 'b'}, {'c', 'd'}};
word = "abcd";
ans = exist(board, word);
System.out.println("demo02 result:" + ans);
}
}