zoukankan      html  css  js  c++  java
  • LeetCode

    题目:

    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.

    For example,
    Given board =

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

    word = "ABCCED", -> returns true,
    word = "SEE", -> returns true,
    word = "ABCB", -> returns false.

    思路:

    递归;及时return true,减少计算量

    package recursion;
    
    public class WordSearch {
    
        public boolean exist(char[][] board, String word) {
            int m;
            int n;
            int wordLen;
            if (board == null || word == null || 
                    (m = board.length) == 0 || (n = board[0].length) == 0 || 
                    (wordLen = word.length()) == 0 || wordLen > m * n) 
                return false;
            boolean[][] visited = new boolean[m][n];
    
            for (int i = 0; i < m; ++i) {
                for (int j = 0; j < n; ++j) {
                    if (dfs(board, i, j, visited, word, 0, m, n, wordLen))
                        return true;
                }
            }
            return false;
        }
     
        private boolean dfs(char[][] board, int x, int y, boolean[][] visited, String word, int pos, int m, int n, int wordLen) {
            if (visited[x][y] || board[x][y] != word.charAt(pos)) return false;
            if (pos == wordLen - 1) return true;
            visited[x][y] = true;
    
            if (x - 1 >= 0 && dfs(board, x - 1, y, visited, word, pos + 1, m, n, wordLen)) return true;
            
            if (x + 1 < m && dfs(board, x + 1, y, visited, word, pos + 1, m, n, wordLen)) return true;
            
            if (y - 1 >= 0 && dfs(board, x, y - 1, visited, word, pos + 1, m, n, wordLen)) return true;
            
            if (y + 1 < n && dfs(board, x, y + 1, visited, word, pos + 1, m, n, wordLen)) return true;
            
            visited[x][y] = false;
            
            return false;
        }
        
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            char[][] board = 
                            {
                                {'A','B','C','E'},
                                {'S','F','C','S'},
                                {'A','D','E','E'}
                            };
            WordSearch w = new WordSearch();
            System.out.println(w.exist(board, "ABCCED"));
            System.out.println(w.exist(board, "SEE"));
            System.out.println(w.exist(board, "ABCB"));
        }
    
    }
  • 相关阅读:
    repr() Vs str()
    默认参数
    元组
    字典
    电脑可以办的两类最基本的事情
    代码块
    变量名
    print的逻辑
    input的逻辑
    让python2听懂中文
  • 原文地址:https://www.cnblogs.com/null00/p/5094747.html
Copyright © 2011-2022 走看看