zoukankan      html  css  js  c++  java
  • 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.

    Analyse: This problem has high time complexity. For each element in the 2D board, do the matching in four directions. If the length of searched elements equals to the string length, return true. If out of board index or the item doesn't equal to the corresponding string[i], return false. 

    Runtime: 92ms

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

    Runtime: 382ms

    class Solution {
    public:
        bool search(vector<vector<char> >& board, string word, int i, int j, int depth, vector<vector<bool> >& used){
            if(i >= board.size() || i < 0 || j >= board[i].size() || j < 0 || board[i][j] != word[depth] || used[i][j])
                return false;
            used[i][j] = true;
            if(depth == word.size() - 1) return true;
            if(search(board, word, i - 1, j, depth + 1, used)  ||
                search(board, word, i + 1, j, depth + 1, used) ||
                search(board, word, i, j - 1, depth + 1, used) ||
                search(board, word, i, j + 1, depth + 1, used))
                return true;
            used[i][j] = false;
            return false;
        }
        bool exist(vector<vector<char>>& board, string word) {
            if(board.empty() || board[0].empty() || word.empty()) return false;
        
            for(int i = 0; i < board.size(); i++){
                for(int j = 0; j < board[i].size(); j++){
                    if(board[i][j] == word[0]){
                        vector<vector<bool > > used(board.size(), vector<bool>(board[0].size(), false));
                        if(search(board, word, i, j, 0, used))
                            return true;
                    }
                }
            }
            return false;
        }
    };
  • 相关阅读:
    彻底卸载网易UU网游加速器的方法
    OO之策略模式
    android锁屏和finish()后activity生命周期的变化
    ARTS打卡计划第6周-TIPS-多台服务器免密码登录
    ARTS打卡计划第6周-REVIEW-超越编码的避免项目失败的软技能
    ARTS打卡计划第6周-ALGORITHM
    ARTS打卡计划第5周-SHARE-java构建树形结构
    ARTS打卡计划第5周-TIPS
    ARTS打卡计划第5周-REVIEW-SpringBoot的api版本化实践
    ARTS打卡计划第5周-ALGORITHM
  • 原文地址:https://www.cnblogs.com/amazingzoe/p/4776980.html
Copyright © 2011-2022 走看看