zoukankan      html  css  js  c++  java
  • LeetCode

    Word Search

    2014.2.26 23:59

    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.

    Solution:

      Simple problem of DFS.

      Total time complexity is O(n^2 * len(word)). Space complexity is O(n^2).

    Accepted code:

     1 // 1CE, 1AC, very smooth.
     2 class Solution {
     3 public:
     4     bool exist(vector<vector<char> > &board, string word) {
     5         n = (int)board.size();
     6         if (n == 0) {
     7             return false;
     8         }
     9         m = (int)board[0].size();
    10         word_len = (int)word.length();
    11         
    12         if (word_len == 0) {
    13             return true;
    14         }
    15         
    16         int i, j;
    17         for (i = 0; i < n; ++i) {
    18             for (j = 0; j < m; ++j) {
    19                 if(dfs(board, word, i, j, 0)) {
    20                     return true;
    21                 }
    22             }
    23         }
    24         return false;
    25     }
    26 private:
    27     int n, m;
    28     int word_len;
    29     
    30     bool dfs(vector<vector<char> > &board, string &word, int x, int y, int idx) {
    31         if (x < 0 || x > n - 1 || y < 0 || y > m - 1) {
    32             return false;
    33         }
    34         
    35         if (board[x][y] < 'A' || board[x][y] != word[idx]) {
    36             // already searched here
    37             // letter mismatch here
    38             return false;
    39         }
    40         
    41         bool res;
    42         if (idx == word_len - 1) {
    43             // reach the end of word, success
    44             return true;
    45         } else {
    46             // up
    47             board[x][y] -= 'A';
    48             res = dfs(board, word, x - 1, y, idx + 1);
    49             board[x][y] += 'A';
    50             if (res) {
    51                 return true;
    52             }
    53             
    54             // down
    55             board[x][y] -= 'A';
    56             res = dfs(board, word, x + 1, y, idx + 1);
    57             board[x][y] += 'A';
    58             if (res) {
    59                 return true;
    60             }
    61             
    62             // left
    63             board[x][y] -= 'A';
    64             res = dfs(board, word, x, y - 1, idx + 1);
    65             board[x][y] += 'A';
    66             if (res) {
    67                 return true;
    68             }
    69             
    70             // right
    71             board[x][y] -= 'A';
    72             res = dfs(board, word, x, y + 1, idx + 1);
    73             board[x][y] += 'A';
    74             if (res) {
    75                 return true;
    76             }
    77         }
    78         // all letters will be within [A-Z], thus I marked a position as 'searched' by setting them to an invalid value.
    79         // we have to restore the value when the DFS is done, so their values must still be distiguishable.
    80         // therefore, I used an offset value of 'A'.
    81         // this tricky way is to save the extra O(n * m) space needed as marker array.
    82         
    83         return false;
    84     }
    85 };
  • 相关阅读:
    RCTF 2019 web
    php花式读取文件
    PHP审计基础
    《笼中鸟——精神病人的生存现状》观后的一点思考
    python一些小trick
    Appium+Python入门学习总结
    解决windows下 Python中 matplotlib 做图中文不显示的问题
    py3.5 telnet的实例(在远程机器上批量创建用户)
    关于pycharm的一个imoprt的随笔
    LookupError: unknown encoding: idna 的处理方法
  • 原文地址:https://www.cnblogs.com/zhuli19901106/p/3570506.html
Copyright © 2011-2022 走看看