zoukankan      html  css  js  c++  java
  • 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.

    For example,
    Given board =

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

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

    版本1, 直接进行DFS。 但是大数据过不去:

    Submission Result: Time Limit Exceeded

     1 public class Solution {
     2     boolean ext = false;
     3     public boolean exist(char[][] board, String word) {
     4         // Start typing your Java solution below
     5         // DO NOT write main() function
     6         ext = false;
     7         if(board == null || board.length == 0 || word == null || word.length() == 0) return ext;
     8         if(board.length * board[0].length < word.length()) return ext;
     9         char c = word.charAt(0);
    10         boolean[][] map = new boolean[board.length][board[0].length];
    11         for(int j = 0; j < board.length; j ++){
    12             for(int i = 0; i < board[0].length; i ++){
    13                 if(board[j][i] == c){
    14                     map[j][i] = true;
    15                     check(board,map,word,1,j,i);
    16                     map[j][i] = false;
    17                 }
    18             }
    19         }
    20         return ext;
    21     }
    22     public void check(char[][] board,boolean[][] map,String word,int n,int j,int i){
    23         if(n == word.length()) {
    24             ext = true;
    25             return;
    26         }
    27         if(j > 0 && map[j-1][i]!= true && board[j][i] == word.charAt(n)){
    28             map[j-1][i] = true;
    29             check(board,map,word,n+1,j-1,i);
    30             map[j-1][i] = false;
    31         }if(j < board.length - 1 && map[j+1][i]!= true && board[j+1][i] == word.charAt(n)){
    32             map[j+1][i] = true;
    33             check(board,map,word,n+1,j+1,i);
    34             map[j+1][i] = false;
    35         }if(i > 0 && map[j][i-1]!= true && board[j][i-1] == word.charAt(n)){
    36             map[j][i-1] = true;
    37             check(board,map,word,n+1,j,i-1);
    38             map[j][i-1] = false;
    39         }if(i < board[0].length - 1 && map[j][i+1]!= true && board[j][i+1] == word.charAt(n)){
    40             map[j][i+1] = true;
    41             check(board,map,word,n+1,j,i+1);
    42             map[j][i+1] = false;
    43         }
    44         return;
    45     }
    46 }

     优化 : 1.当得到一个可行解之后剪枝,直接返回true。 (仍然过不了大测试)

              2.将两个二维数组合并,这样每次只需要复制一个数组。(过了)

     1 public class Solution {
     2     public boolean exist(char[][] board, String word) {
     3         // Start typing your Java solution below
     4         // DO NOT write main() function
     5         if(board == null || board.length == 0 || word == null || word.length() == 0) return false;
     6         if(board.length * board[0].length < word.length()) return false;
     7         char c = word.charAt(0);
     8         for(int j = 0; j < board.length; j ++){
     9             for(int i = 0; i < board[j].length; i ++){
    10                 if(board[j][i] == c){
    11                     board[j][i] = 0;
    12                     boolean result = check(board,word,1,j,i);
    13                     if(result == true) return true;
    14                     board[j][i] = c;
    15                 }
    16             }
    17         }
    18         return false;
    19     }
    20     public boolean check(char[][] board,String word,int n,int j,int i){
    21         if(n == word.length()) {
    22             return true;
    23         }
    24         boolean result = false;
    25         char c = word.charAt(n);
    26         if(j > 0 && board[j-1][i] == c){
    27             board[j-1][i] = 0;
    28             result = check(board,word,n+1,j-1,i);
    29             if(result == true) return true;
    30             board[j-1][i] = c;
    31         }if(j < board.length - 1 && board[j+1][i] == c){
    32             board[j+1][i] = 0;
    33             result = check(board,word,n+1,j+1,i);
    34             if(result == true) return true;
    35             board[j+1][i] = c;
    36         }if(i > 0 && board[j][i-1] == c){
    37             board[j][i-1] = 0;
    38             result = check(board,word,n+1,j,i-1);
    39             if(result == true) return true;
    40             board[j][i-1] = c;
    41         }if(i < board[j].length - 1 && board[j][i+1] == c){
    42             board[j][i+1] = 0;
    43             result = check(board,word,n+1,j,i+1);
    44             if(result == true) return true;
    45             board[j][i+1] = c;
    46         }
    47         return false;
    48     }
    49 }
  • 相关阅读:
    在Windows环境中使用版本管理工具Git [转]
    poj3630 Phone List **
    UVa10382 Watering Grass **
    软件项目计划书格式 [转]
    POI2001 Ants and the ladybug ***
    RMQ+1/1算法 [转]
    poj3264 Balanced Lineup *
    Windows Server 2003 出现的Service Unavailable怎么办?
    单车环佛山一环
    天凉了,大家多穿衣服
  • 原文地址:https://www.cnblogs.com/reynold-lei/p/3342508.html
Copyright © 2011-2022 走看看