leetcode Brush title notes - word search

Subject description:

Given a two-dimensional grid and a word, to find out whether the word exists in the grid.

Words must, by the letters in adjacent cells constituting alphabetically, where "adjacent" cells that are adjacent horizontally or vertically adjacent cells. Letters within the same cell is not allowed to be reused.

Example:

board =
[
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
]

Given word = "ABCCED", returns true.
Given word = "SEE", returns true.
Given word = "ABCB", returns false.

Problem-solving ideas:

And backtracking depth-first search (dfs)

First Edition Code:

The first edition was written by himself, thought of backtracking, but did not want to understand dfs. Specific code as follows, idea is to traverse the two-dimensional array, find the location of the first letter in the word, and from that position, the temptation, the letter down, left, and right positions if the next letter in the word matching, if yes, the position as a new starting position, moves back a word letter coordinates, recursive, and with flag [i] (0 <= i <= 3) is recorded if the word matches the path.

But this line of thinking will result in a timeout, because it will all len two-dimensional array of paths (word) length traversal again, when the array size is too large and when the word length is longer, the situation is too long when used will appear. So it should go one way, if this road and word match, and then immediately returns true, do not need to consider other paths up.

class Solution {
    public boolean exist(char[][] board, String word) {
        if(board == null || board.length == 0)
            return false;

        for(int i=0; i<board.length; i++){
            for (int j=0; j<board[0].length; j++){
                if(word.charAt(0) == board[i][j]){
       
int[][] mark = new int[board.length][board[0].length]; mark[i][j] = 1; if(func(board, word, 1, i, j, mark)) return true; } } } return false; } public boolean func(char[][] board, String word, int pos, int row, int col, int[][] mark){ if(pos == word.length()) return true; boolean[] flag = new boolean[4]; if(row > 0 && mark[row-1][col] == 0){ if(board[row-1][col] == word.charAt(pos)){ mark[row-1][col] = 1; flag[0] = func(board, word, pos+1, row-1, col, mark); mark[row-1][col] = 0; } } if (row < board.length-1 && mark[row+1][col] == 0) { if (board[row + 1][col] == word.charAt(pos)){ mark[row+1][col] = 1; flag[1] = func(board, word, pos+1, row+1, col, mark); mark[row+1][col] = 0; } } if(col > 0 && mark[row][col-1] == 0) { if (board[row][col - 1] == word.charAt(pos)){ mark[row][col-1] = 1; flag[2] = func(board, word, pos+1, row, col-1, mark); mark[row][col-1] = 0; } } if(col < board[0].length-1 && mark[row][col+1] == 0){ if(board[row][col+1] == word.charAt(pos)){ mark[row][col+1] = 1; flag[3] = func(board, word, pos+1, row, col+1, mark); mark[row][col+1] = 0; } } if(flag[0] || flag[1] || flag[2] || flag[3]) return true; return false; } }

Second Edition Code:

Is changed after reading the explanations in the first version of the code above, the idea of ​​dfs is added, removed In Flag [] of this flag, and if the word matches this path, directly returns

public boolean exist(char[][] board, String word) {
        if(board == null || board.length == 0)
            return false;

        for ( int I = 0; I <board.length; I ++ ) {
             for ( int J = 0; J <Board [0] .length; J ++ ) {
                 IF (word.charAt (0) == Board [I] [ J]) {
                     // record whether this position has passed 
                    int [] [] = Mark new new  int [board.length] [Board [0 ] .length];
                    mark[i][j] = 1;
                    if(func(board, word, 1, i, j, mark))
                        return true;
                }
            }
        }
        return false;
    }

    public boolean func(char[][] board, String word, int pos, int row, int col, int[][] mark){
        if(pos == word.length()) return true;
        boolean[] flag = new boolean[4];
        if(row > 0 && mark[row-1][col] == 0){
            if(board[row-1][col] == word.charAt(pos)){
                mark[row-1][col] = 1;
                //递归
                if(func(board, word, pos+1, row-1, col, mark))
                    return true;
                //回溯
                mark[row-1][col] = 0;
            }

        }
        if (row < board.length-1 && mark[row+1][col] == 0) {
            if (board[row + 1][col] == word.charAt(pos)){
                mark[row+1][col] = 1;
                if(func(board, word, pos+1, row+1, col, mark))
                    return true;
                mark[row+1][col] = 0;
            }

        }
        if(col > 0 && mark[row][col-1] == 0) {
            if (board[row][col - 1] == word.charAt(pos)){
                mark[row][col-1] = 1;
                if(func(board, word, pos+1, row, col-1, mark))
                    return true;
                mark[row][col-1] = 0;
            }

        }
        if(col < board[0].length-1 && mark[row][col+1] == 0){
            if(board[row][col+1] == word.charAt(pos)){
                mark[row][col+1] = 1;
                if(func(board, word, pos+1, row, col+1, mark))
                    return true;
                mark[row][col+1] = 0;
            }

        }
        return false;
    }

 

Guess you like

Origin www.cnblogs.com/yingying7/p/11331066.html