Boostable

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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.

地址:https://oj.leetcode.com/problems/word-search/

算法:用DFS加回溯法来解决。首先,从board的每一个位置开始搜索,假设当前遍历到第(i,j)个位置,如果word的第一个字符等于board[i][j],那么将以遍历的位置置为true,然后调用函数existCore进行DFS,existCore函数的最后三个参数表示,当前遍历到第(i,j)个位置,并且等待匹配word的第pos个字符。在existCore函数里面,如果已经匹配到word的最后一个词,则说明匹配完成,否则从(i,j)位置开始,往上下左右四个位置开始遍历,若满足条件,则递归调用existCore。注意,若往某个方向发现最终找不到解,则要将对应的flag位置清除,回溯到原来位置。代码:

 1 class Solution {
 2 public:
 3     bool exist(vector<vector<char> > &board, string word) {
 4         if(board.empty())   return false;
 5         if(word.empty())    return true;
 6         int m = board.size();
 7         vector<vector<bool> > flag;
 8         for(int i = 0; i < m; ++i){
 9             int n = board[i].size();
10             flag.push_back(vector<bool>(n,false));
11         }
12         for(int i = 0; i < m; ++i){
13             int n = board[i].size();
14             for(int j = 0; j < n; ++j){
15                 if(word[0] == board[i][j]){
16                     flag[i][j] = true;
17                     if(existCore(board,word,flag,i,j,1))
18                         return true;
19                     flag[i][j] = false;
20                 }
21             }
22         }
23         return false;
24     }
25     bool existCore(vector<vector<char> > &board, string &word, vector<vector<bool> > &flag, int i, int j, int pos){
26         if(pos == word.size()){
27             return true;
28         }
29         if(i > 0 && j < board[i-1].size() && !flag[i-1][j] && board[i-1][j] == word[pos]){
30             flag[i-1][j] = true;
31             if(existCore(board,word,flag,i-1,j,pos+1)){
32                 return true;
33             }
34             flag[i-1][j] = false;
35         }
36         if(j < board[i].size() - 1 && !flag[i][j+1] && board[i][j+1] == word[pos]){
37             flag[i][j+1] = true;
38             if(existCore(board,word,flag,i,j+1,pos+1)){
39                 return true;
40             }
41             flag[i][j+1] = false;
42         }
43         if(i < board.size() - 1 && j < board[i+1].size() && !flag[i+1][j] && board[i+1][j] == word[pos]){
44             flag[i+1][j] = true;
45             if(existCore(board,word,flag,i+1,j,pos+1)){
46                 return true;
47             }
48             flag[i+1][j] = false;
49         }
50         if(j > 0 && !flag[i][j-1] && board[i][j-1] == word[pos]){
51             flag[i][j-1] = true;
52             if(existCore(board,word,flag,i,j-1,pos+1)){
53                 return true;
54             }
55             flag[i][j-1] = false;
56         }
57         return false;
58     }
59 };

 

posted on 2014-09-04 20:48  Boostable  阅读(322)  评论(0编辑  收藏  举报