Leetcode-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.

Analysis:

Typical DFS problem. Need to consider the step of backtracking.

Solution:

 1 public class Solution {
 2     public boolean exist(char[][] board, String word) {
 3         int xLen = board.length;
 4         if (xLen==0) return false;
 5         int yLen = board[0].length;
 6         if (yLen==0) return false;
 7 
 8         boolean[][] visited = new boolean[xLen][yLen];
 9         for (int i=0;i<xLen;i++)
10             for (int j=0;j<yLen;j++)
11                 visited[i][j] = false;
12  
13         char target = word.charAt(0);
14         for (int i=0;i<xLen;i++)
15             for (int j=0;j<yLen;j++)
16                 if (board[i][j]==target){
17                     //NOTE:We should consider about the case that the word.length is 1.
18                     if (word.length()==1) return true;
19                     visited[i][j]=true;
20                     boolean find = searchRecur(board,visited,i,j,1,word);
21                     visited[i][j]=false;
22                     if (find) return true;
23                 }
24         return false;
25     }
26 
27     public boolean searchRecur(char[][] board, boolean[][] visited, int x, int y, int cur, String word){
28         int xLen = board.length;
29         int yLen = board[0].length;
30         char target = word.charAt(cur);
31         if (cur==word.length()-1){
32             if (x+1<xLen && board[x+1][y]==target && !visited[x+1][y]) return true;
33             if (y+1<yLen && board[x][y+1]==target && !visited[x][y+1]) return true;
34             if (x-1>=0 && board[x-1][y]==target && !visited[x-1][y]) return true;
35             if (y-1>=0 && board[x][y-1]==target && !visited[x][y-1]) return true;
36             return false;
37         }
38 
39 
40         if (x+1<xLen && board[x+1][y]==target && !visited[x+1][y]){
41             visited[x+1][y] = true;
42             boolean next = searchRecur(board,visited,x+1,y,cur+1,word);
43             visited[x+1][y] = false;
44             if (next) return true;
45         }
46 
47         if (y+1<yLen && board[x][y+1]==target && !visited[x][y+1]){
48             visited[x][y+1] = true;
49             boolean next = searchRecur(board,visited,x,y+1,cur+1,word);
50             visited[x][y+1] = false;
51             if (next) return true;
52         }
53 
54         if (x-1>=0 && board[x-1][y]==target && !visited[x-1][y]){
55             visited[x-1][y] = true;
56             boolean next = searchRecur(board,visited,x-1,y,cur+1,word);
57             visited[x-1][y] = false;
58             if (next) return true;
59         }
60 
61         if (y-1>=0 && board[x][y-1]==target && !visited[x][y-1]){
62             visited[x][y-1] = true;
63             boolean next = searchRecur(board,visited,x,y-1,cur+1,word);
64             visited[x][y-1] = false;
65             if (next) return true;
66         }
67   
68         return false;
69     }
70 }

 

posted @ 2014-11-16 08:40  LiBlog  阅读(151)  评论(0编辑  收藏  举报