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

fk! 被两个问题卡住了, 一个是要先帮search函数找到入口,开始我偷懒想试图让程序自己找入口,结果就悲剧了,还找了半天bug,还有一个问题就是已经得到函数结果要尽快return,因为是递归函数,不尽快return的话,后面还会继续递归,然后就华丽丽的超时了。

 1 class Soluton {
 2 public:
 3     bool search(vector<vector<char> > &board, string &word, vector<vector<bool> > &mask, int idx, int x, int y) {
 4         if (word[idx] == board[x][y]) {
 5             ++idx;
 6             if (idx == word.length()){
 7                 return true;
 8             }
 9         } else {
10             return false;
11         }
12         mask[x][y] = false;
13         bool flag1, flag2, flag3, flag4;
14         flag1 = flag2 = flag3 = flag4 = false;
15         if (y + 1 < board[0].size() && mask[x][y+1] && board[x][y+1] == word[idx]) {
16             if (search(board, word, mask, idx, x, y + 1))
17                 return true;
18         }
19         if (x + 1 < board.size() && mask[x+1][y] && board[x+1][y] == word[idx]) {
20            if (search(board, word, mask, idx, x + 1, y))
21                 return true;
22         }
23         if (x - 1 >= 0 && mask[x-1][y] && board[x-1][y] == word[idx]) {
24            if (search(board, word, mask, idx, x - 1, y))
25                 return true;
26         }
27         if (y - 1 >= 0 && mask[x][y-1] && board[x][y-1] == word[idx]) {
28            if (search(board, word, mask, idx, x, y - 1))
29                 return true;
30         }
31         mask[x][y] = true;
32         return false;
33     }
34     
35     bool exist(vector<vector<char> > &board, string word) {
36         vector<vector<bool> > mask(board.size(), vector<bool>(board[0].size(), true));
37         if (board.size() < 1) return false;
38         for (int i = 0; i <board.size(); ++i) {
39             for (int j = 0; j < board[0].size(); ++j) {
40                 if (board[i][j] == word[0] && search(board, word, mask, 0, i, j)) {
41                     return true;
42                 }
43             }
44         }
45         return false;
46     }
47 };

 刷第二遍,代码可以再简洁一点,要尽量避免重复的代码。

 1 class Solution {
 2 public:
 3     bool isValid(vector<vector<char>> &board, vector<vector<bool>> &visit, int x, int y) {
 4         int m = board.size(), n = board[0].size();
 5         if (x < 0 || x >= m || y < 0 || y >= n || visit[x][y]) return false;
 6         return true;
 7     }
 8     
 9     bool dfs(vector<vector<char>> &board, vector<vector<bool>> &visit, string word, int idx, int x, int y) {
10         if (idx == word.length()) return true;
11         int dx[4] = {1, 0, -1, 0};
12         int dy[4] = {0, 1, 0, -1};
13         visit[x][y] = true;
14         int xx, yy;
15         for (int i = 0; i < 4; ++i) {
16             xx = x + dx[i]; yy = y + dy[i];
17             if (isValid(board, visit, xx, yy) && board[xx][yy] == word[idx] && dfs(board, visit, word, idx + 1, xx, yy)) {
18                 return true;
19             }
20         }
21         visit[x][y] = false;
22         return false;
23     }
24     
25     bool exist(vector<vector<char>>& board, string word) {
26         if (word.empty()) return false;
27         int m = board.size();
28         if (m == 0) return false;
29         int n = board[0].size();
30         if (n == 0) return false;
31         vector<vector<bool>> visit(m, vector<bool>(n, false));
32         for (int i = 0; i < m; ++i) {
33             for (int j = 0; j < n; ++j) {
34                 if (board[i][j] == word[0] && dfs(board, visit, word, 1, i, j)) return true;
35             }
36         }
37         return false;
38     }
39 };

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值