/*** @param {character[][]} board* @param {string} word* @return {boolean}*/// 思路// dfs四个方向的或值 并返回// dfs 什么时候进入// dfs 返回值 长度相等时
var exist =function(board, word){const m = board.length;const n = board[0].length;for(let i =0; i < m; i++){for(let j =0; j < n; j++){if(board[i][j]=== word[0]){if(dfs(0, i, j))returntrue;}}}returnfalse;functiondfs(idx, x, y){if(x <0|| x >= m || y <0|| y >= n)returnfalse;if(board[x][y]!== word[idx])returnfalse;if(idx === word.length -1)returntrue;board[x][y]=null;const res =dfs(idx +1, x +1, y)||dfs(idx +1, x -1, y)||dfs(idx +1, x, y +1)||dfs(idx +1, x, y -1);board[x][y]= word[idx];return res;}};console.log(exist([["A","B","C","E"],["S","F","C","S"],["A","D","E","E"],],"ABCCED"));
console.log(exist([["A","B","C","E"],["S","F","C","S"],["A","D","E","E"],],"ABCB"));// board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"// [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"