zoukankan      html  css  js  c++  java
  • [LeetCode] Word Search

    This problem is somewhat tricky at first glance. However, the final implementation is fairly simple using recursion.

    The basic idea is, visiting every possible position (i, j) of  board and find if starting from (i, j), the word exists in the board. For the word to exist starting from (i, j), two conditions should be satisfied:

    1. board[i][j] = word[0] ;
    2. Define tail = word.substr(1, word.length() - 1) , then we should be able to find tail starting from one of the four neighbors of (i, j), which requires a recursive call of the function.

    Putting 1 and 2 together, we have the following code, which is self-explanatory.

     1 class Solution {
     2 public:
     3     bool exist(vector<vector<char>> board, string word) {
     4         for (int i = 0; i < board.size(); i++)
     5             for (int j = 0; j < board[0].size(); j++)
     6                 if (isExist(board, word, i, j)) return true;
     7         return false;
     8     }
     9 private:
    10     bool isExist(vector<vector<char>>& board, string& word, int row, int col) {
    11         if (row < 0 || row >= board.size() || col < 0 || col >= board[0].size() || board[row][col] != word[0])
    12             return false;
    13         if (word.length() == 1) return true;
    14         board[row][col] = ' ';
    15         string tail = word.substr(1, word.length() - 1);
    16         if (isExist(board, tail, row - 1, col) || isExist(board, tail, row + 1, col) ||
    17             isExist(board, tail, row, col - 1) || isExist(board, tail, row, col + 1))
    18             return true;
    19         board[row][col] = word[0];
    20         return false;
    21     }
    22 };

    Note that in the above code, in order to prevent visiting the same grid of board again, we modify the starting point of the grid and recover it again in lines 14 and 19 respectively.

    The above code is readable, though not fast enough. A simple trick to make it faster to pass a char*  instead of string to the isExist function.

     1 class Solution {
     2 public:
     3     bool exist(vector<vector<char>> board, string word) {
     4         for (int i = 0; i < board.size(); i++)
     5             for (int j = 0; j < board[0].size(); j++)
     6                 if (isExist(board, word.c_str(), i, j)) return true;
     7         return false;
     8     }
     9 private:
    10     bool isExist(vector<vector<char>>& board, const char* word, int row, int col) {
    11         if (row < 0 || row >= board.size() || col < 0 || col >= board[0].size() || board[row][col] != *word)
    12             return false;
    13         if (!(*(word + 1))) return true;
    14         board[row][col] = ' ';
    15         if (isExist(board, word + 1, row - 1, col) || isExist(board, word + 1, row + 1, col) ||
    16             isExist(board, word + 1, row, col - 1) || isExist(board, word + 1, row, col + 1))
    17             return true;
    18         board[row][col] = *word;
    19         return false;
    20     }
    21 };
  • 相关阅读:
    ExtJS 刷新或者重载Tree后,默认选中刷新前最后一次选中的节点代码片段
    ios>APP名称的多语言化(转)
    android>apk破解以及重新编译(转)
    MFC动态库基本概念
    (内存中的)堆和栈的区别(转过无数次的文章)
    面向对象五大基本原则
    VS20052008程序发布、打包(MFC)
    在MFC中创建动态控件的生成与响应
    SQL2000自动备份数据库并发送邮件报告数据库自动备份情况
    The Concept of Callbacks
  • 原文地址:https://www.cnblogs.com/jcliBlogger/p/4550994.html
Copyright © 2011-2022 走看看