zoukankan      html  css  js  c++  java
  • 79. Word Search

    Given a 2D board and a word, find if the word exists in the grid.

    The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.

    Example:

    board =
    [
      ['A','B','C','E'],
      ['S','F','C','S'],
      ['A','D','E','E']
    ]
    
    Given word = "ABCCED", return true.
    Given word = "SEE", return true.
    Given word = "ABCB", return false.

    又没申清题意,没有注意adjacent这个关键词。想用一个Boolean数组记录,我就说这题怎么可能这么简单。
    然后呢,大佬们纷纷给出解答,比较多的是dfs,我一看,嗬,这不是回溯吗,再查了一下

    class Solution {
        public boolean exist(char[][] board, String word) {
            int m = board.length;
            int n = board[0].length;
            boolean[][] visited = new boolean[m][n];
            for(int i = 0; i < m; i++){//Need traverse all possible i and j until get result
                for(int j = 0; j < n; j++){
                    if(dfs(board,word,0,i,j,visited)) return true;
                }
            }
            return false;
        }
        public boolean dfs(char[][] board, String word, int index, int x, int y,boolean[][] visited){
            if(index==word.length()) return true;   //Means get what needed
            
            if(x<0||x==board.length||y<0||y==board[0].length) return false; //Means go beyond the boundary
             if(visited[x][y]) return false;    //Single element can only be accessed once in a dfs
            if(board[x][y] != word.charAt(index)) return false;
            visited[x][y]= true;    //Means current element never been accessed and existed in word.
                if(dfs(board, word, index+1, x+1,y,visited)||//right
                   dfs(board, word, index+1, x-1,y,visited)||//left
                   dfs(board, word, index+1, x,y+1,visited)||//up
                   dfs(board, word, index+1, x,y-1,visited)){//down
                    return true;
                }
                   visited[x][y] = false;   //Release element xy to be accessed by next dfs
                   return false;
        }
    }
        private static int m, n;
        private static final int[][] dirs = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
        
        public boolean exist(char[][] board, String word) {
            if (word == null || word.length() == 0)
                return true;
            
            m = board.length; 
            n = board[0].length;
            char firstChar = word.charAt(0);
            
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    if (board[i][j] == firstChar) {
                        // Mark as visited.
                        board[i][j] = '-';
                        if (searchFrom(i, j, board, word, 1))
                            return true;
                        // Restore to unvisited.
                        board[i][j] = firstChar;
                    }
                }
            }
                
            return false;
        }
        
        private static boolean searchFrom(int x, int y, char[][] board, String word, int wi) {        
            // Acception case, leaf.
            if (wi == word.length()) 
                return true;
            
            for (int[] dir : dirs) {
                int nx = x + dir[0];
                int ny = y + dir[1];
                
                if (nx < 0 || nx >= m || ny < 0 || ny >= n || word.charAt(wi) != board[nx][ny])
                    continue;
                // Mark as visited.
                board[nx][ny] = '-';
                if (searchFrom(nx, ny, board, word, wi + 1))
                    return true;
                // Restore to unvisited.
                board[nx][ny] = word.charAt(wi);
            }
            
            return false;
        }

    不能更帅

    
    
  • 相关阅读:
    mysql 模糊查询LIKE 在tp中使用
    json字符串与 js对象互相转换
    1431. Kids With the Greatest Number of Candies
    1481. Least Number of Unique Integers after K Removals
    560. Subarray Sum Equals K
    1476. Subrectangle Queries
    1475. Final Prices With a Special Discount in a Shop
    网速和流量有什么关系
    计算网速的计算公式是什么
    php file_put_contents 函数的使用
  • 原文地址:https://www.cnblogs.com/wentiliangkaihua/p/10482687.html
Copyright © 2011-2022 走看看