请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一格开始,每一步可以在矩阵中向左、右、上、下移动一格。如果一条路径经过了矩阵的某一格,那么该路径不能再次进入该格子。例如,在下面的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
提示:
1 <= board.length <= 200
1 <= board[i].length <= 200
参考
利用深度优先和回溯法
/**
* @param {character[][]} board
* @param {string} word
* @return {boolean}
*/
var exist = function(board, word) {
const rowNum = board.length;
const colNum = board[0].length;
for (let i = 0; i < rowNum; ++i) {
for (let j = 0; j < colNum; ++j) {
if (board[i][j] === word[0]) {
const isExist = __exist(board, word, i, j, {});
if (isExist) return true; // 找到就返回
}
}
}
return false;
};
/**
* @param {character[][]} board
* @param {string} word
* @param {number} row
* @param {number} col
* @param {object} visited
* @return {boolean}
*/
function __exist(board, word, row, col, visited) {
// 单词中字母全部匹配,说明可以搜索到,返回true
if (!word.length) {
return true;
}
const key = `${row}-${col}`;
// 越界、之前访问过、单词首字母和当前元素不相同,返回false
if (
row >= board.length ||
row < 0 ||
col >= board[0].length ||
col < 0 ||
visited[key] ||
board[row][col] !== word[0]
) {
return false;
}
visited[key] = true;
word = word.slice(1);
// 下、上、右、左搜索(顺序不重要)
const success =
__exist(board, word, row + 1, col, visited) ||
__exist(board, word, row - 1, col, visited) ||
__exist(board, word, row, col + 1, visited) ||
__exist(board, word, row, col - 1, visited);
// success为false时,就是回溯
visited[key] = success;
return success;
}