Monday, January 14, 2013

[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.
» Solve this problem

[解题思路]
一道递归题。跟前面一道robot路径的问题一样,只不过取值变成了上下左右四个方向,而不是仅仅下右两个方向。

这里加了一个visited的数组,用于避免重复统计字母。比如如果word在board里面是个环的话。

[Code]
1:    bool exist(vector<vector<char> > &board, string word) {  
2:      // Start typing your C/C++ solution below  
3:      // DO NOT write int main() function  
4:      if(word.size() ==0) return false;  
5:      if(board.size() ==0 || board[0].size() == 0) return false;  
6:      int row = board.size();  
7:      int col = board[0].size();  
8:      int * visited = new int[row*col];  
9:      memset(visited, 0, row*col*sizeof(int));  
10:      for(int i =0; i< board.size(); i++)  
11:      {  
12:        for(int j =0; j< board[0].size(); j++)  
13:        {  
14:          if(board[i][j] == word[0])  
15:          {  
16:            visited[i*col+j] = 1;  
17:            if(search(board, word, visited, -1, 1, i, j))  
18:              return true;  
19:            visited[i*col+j] =0;  
20:          }  
21:        }  
22:      }  
23:      delete visited;  
24:      return false;  
25:    }    
26:    bool search(vector<vector<char> > &board,   
27:      string& word,  
28:      int* visited,  
29:      int op, //0 up, 1 down, 2 left, 3 right  
30:      int matchLen,  
31:      int i,  
32:      int j)  
33:    {  
34:      if(matchLen == word.size()) return true;  
35:      int row = board.size();  
36:      int col = board[0].size();  
37:      if(i+1<row && op!=0)  
38:      {  
39:        if(visited[(i+1)*col+j] ==0 &&   
40:        board[i+1][j] == word[matchLen])  
41:        {  
42:          visited[(i+1)*col+j] =1;  
43:          if(search(board, word, visited, 1, matchLen+1, i+1, j))  
44:            return true;  
45:          visited[(i+1)*col+j] =0;  
46:        }  
47:      }  
48:      if(i-1>=0 && op!=1)  
49:      {  
50:        if(visited[(i-1)*col+j] ==0 && board[i-1][j] == word[matchLen])  
51:        {  
52:          visited[(i-1)*col+j] =1;  
53:          if(search(board, word, visited, 0, matchLen+1, i-1, j))  
54:            return true;  
55:          visited[(i-1)*col+j] =0;  
56:        }  
57:      }  
58:      if(j+1<col && op!=2)  
59:      {  
60:        if(visited[i*col+j+1] ==0 && board[i][j+1] == word[matchLen])  
61:        {  
62:          visited[i*col+j+1] =1;  
63:          if(search(board, word, visited, 3, matchLen+1, i, j+1))  
64:            return true;  
65:          visited[i*col+j+1] =0;  
66:        }  
67:      }  
68:      if(j-1>=0 && op!=3)  
69:      {  
70:        if(visited[i*col+j-1] ==0 && board[i][j-1] == word[matchLen])  
71:        {  
72:          visited[i*col+j-1] =1;  
73:          if(search(board, word, visited, 2, matchLen+1, i, j-1))  
74:            return true;  
75:          visited[i*col+j-1] =0;  
76:        }  
77:      }  
78:      return false;      
79:    }  






No comments: