2

[Leetcode 346/700] 79. Word Search [Medium] Backtracking Deep Search JavaScript Version

1. Topic

n a two-dimensional character grid board and a string word word. Returns true if word exists in the grid; otherwise, returns false.

Words must be formed by letters in adjacent cells in alphabetical order, where "adjacent" cells are those that are either horizontally or vertically adjacent. Letters in the same cell are not allowed to be reused.

Example 1:

 输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
输出:true

Example 2:

 输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
输出:true

Example 3:

 输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
输出:false

hint:

 m == board.length
    n = board[i].length
    1 <= m, n <= 6
    1 <= word.length <= 15
    board 和 word 仅由大小写英文字母组成

2 Problem solving ideas

1. Traverse all the elements of the board, find the first identical element of the word, and mark it, enter the recursion to find the next second character, and then the third letter. If not found, return false;

  1. A backtracking search is performed within the set boundaries, that is, the search for the next character is performed up, down, left, and right. If it is found and enter the new recursion, if it is not found, it will return false directly;

3. Problem solving points

1. Timely mark the status of characters, whether they have been accessed or not;
2. If all the strings are intercepted at the end, it means that a matching answer has been found, and returns true directly;

4. Problem solving code

 /**
 * @param {character[][]} board
 * @param {string} word
 * @return {boolean}
 */
var exist = function (board, word) {
    let border = [[0, 1], [0, -1], [1, 0], [-1, 0]], //定义上下左右四个方向
        col = board.length, //行数
        row = board[0].length, //列数
        marked = [...Array(col)].map(v => Array(row).fill()); //同行列空矩阵,用于记录已经访问的

        //空数组直接返回false
    if (!col) return false;

    let backTracing = (i, j, markeds, boards, words) => {
        //截取所有的字符,说明已经找到
        if (!words.length) {
            return true;
        }
        for (let p = 0; p < border.length; p++) {
            let curi = i + border[p][0]; //左右方向
            let curj = j + border[p][1]; //上下方向

            //判断边界,且找到了第一个字符
            if ((curi >= 0 && curi < col) && (curj >= 0 && curj < row && boards[curi][curj] == words[0])) {
                //已经用过,直接跳过
                if (markeds[curi][curj] == 1) {
                    continue
                }
                //标记为已使用
                markeds[curi][curj] = 1;
                //接着找下一个字符
                if (backTracing(curi, curj, markeds, boards, words.substring(1))) {
                    return true
                } else {
                    //使用完重置掉
                    markeds[curi][curj] = 0;
                }
            }
        }
        return false
    }

    for (let i = 0; i < col; i++) {
        for (let j = 0; j < row; j++) {
            if (board[i][j] === word[0]) {
                //找到第一个字符,标记为已经使用
                marked[i][j] = 1;
                //进入回溯
                if (backTracing(i, j, marked, board, word.substring(1))) {
                    return true
                } else {
                    //重置状态
                    marked[i][j] = 0;
                }
            }
        }
    }
    return false
};
//测试用例 1
let board = [["A", "B", "C", "E"], ["S", "F", "C", "S"], ["A", "D", "E", "E"]], word = "ABCCED";

//测试用例2

let board1 = [["A", "B", "C", "E"], ["S", "F", "C", "S"], ["A", "D", "E", "E"]], word1 = "SEE"
//测试用例3
let board2 = [["A", "B", "C", "E"], ["S", "F", "C", "S"], ["A", "D", "E", "E"]], word2 = "ABCB"

console.log(exist(board, word))  //true
console.log(exist(board1, word1)) //true
console.log(exist(board2, word2)) //false

微芒不朽
1.2k 声望1.3k 粉丝