zoukankan      html  css  js  c++  java
  • [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 };
  • 相关阅读:
    mongodb简介
    tomcat简介
    date的详细说明
    30岁前成功的12条黄金法则
    2012春晚经典语录
    统计文件中某个单词出现的次数
    nginx简介
    ATM取款机系统模拟仿真
    十三种时间管理方法
    笔试常见的智力题 附答案
  • 原文地址:https://www.cnblogs.com/easonliu/p/3647880.html
Copyright © 2011-2022 走看看