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 };
  • 相关阅读:
    yum 崩溃的解决方法
    线上mysql数据库删库恢复的案例
    CVE-2018-8120 WIN7 08提权漏洞exp
    CVE-2018-1111 劫持dhcp造成Redhat、centos代码执行
    关于 Python generator(生成器)的类比
    黯淡蓝点:旅行者号64亿公里外回望地球...
    raise RuntimeError("autoconf error") RuntimeError: autoconf error
    python 调取 shell 命令的几种方法
    struct 处理二进制
    linux 下日常使用便利工具
  • 原文地址:https://www.cnblogs.com/easonliu/p/3647880.html
Copyright © 2011-2022 走看看