zoukankan      html  css  js  c++  java
  • Word Search 解答

    Question

    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 =

    [
      ["ABCE"],
      ["SFCS"],
      ["ADEE"]
    ]
    

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

    Solution

    Because we have four choices to go, so this problem is not suitable to be solved by DP.

    We use DFS here to traverse all possible paths.

    One tricky note here is to check whether target equals to board[i][j] at every begining.

     1 public class Solution {
     2     public int[][] directions = {{0, 1},{0, -1},{1, 0},{-1, 0}};
     3     
     4     public boolean exist(char[][] board, String word) {
     5         int m = board.length, n = board[0].length;
     6         boolean[][] visited = new boolean[m][n];
     7         for (int i = 0; i < m; i++)
     8             Arrays.fill(visited[i], false);
     9         char target = word.charAt(0);
    10         for (int i = 0; i < m; i++) {
    11             for (int j = 0; j < n; j++) {
    12                 if (dfsSearch(board, word, 0, visited, i, j))
    13                     return true;
    14             }
    15         }
    16         return false;
    17     }
    18     
    19     private boolean dfsSearch(char[][] board, String word, int index, boolean[][] visited, int startX, int startY) {
    20         if (index >= word.length())
    21             return true;
    22         int m = board.length, n = board[0].length;
    23         if (startX < 0 || startX >= m || startY < 0 || startY >= n)
    24             return false;
    25         char target = word.charAt(index);
    26         if (board[startX][startY] != target)
    27             return false;
    28         if (visited[startX][startY])
    29             return false;
    30         visited[startX][startY] = true;
    31         
    32         // Traverse four directions
    33         boolean result = dfsSearch(board, word, index + 1, visited, startX, startY + 1) ||
    34                         dfsSearch(board, word, index + 1, visited, startX, startY - 1) ||
    35                         dfsSearch(board, word, index + 1, visited, startX + 1, startY) ||
    36                         dfsSearch(board, word, index + 1, visited, startX - 1, startY);
    37         // Reset visited[startX][startY]
    38         visited[startX][startY] = false;
    39         return result;
    40     }
    41 }
  • 相关阅读:
    操作系统死锁原因及必要条件
    微信公众平台实现获取用户OpenID的方法
    开源授权协议使用调查报告,Apache最受欢迎 狼人:
    软件工程师,你真的喜欢你的工作吗? 狼人:
    程序员的本质 狼人:
    推荐16个最流行的JavaScript框架 狼人:
    11个完全免费的线框图工具 狼人:
    消息队列软件产品大比拼 狼人:
    FreeMarker 2.3.17版发布! 狼人:
    分享9个实用的jQuery倒计时插件 狼人:
  • 原文地址:https://www.cnblogs.com/ireneyanglan/p/4876325.html
Copyright © 2011-2022 走看看