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.
    

      AC 代码:

    class Solution {
    public:
       bool search(vector<vector<char> > &board, const string & word, int i, int j, int index, vector<vector<bool>> &flag)
        {
              if(index == len) return true;
              //up
              if(i-1 >=0 && board[i-1][j] == word[index] && flag[i-1][j] == false) { 
                 flag[i-1][j] = true;
                 if(search(board, word, i-1, j,index+1, flag) )  return true;
                 flag[i-1][j] = false;
              }
              //down
              if(i+1 < rows && board[i+1][j] == word[index] && flag[i+1][j] == false){
                flag[i+1][j] = true;
                if(search(board, word, i+1, j, index+1, flag)) return true;
                flag[i+1][j] = false;
              }
             //right
             if( j+1 < columns && board[i][j+1] == word[index] && flag[i][j+1] == false){
                flag[i][j+1] = true;
                if(search(board, word, i, j+1, index+1, flag)) return true;
                flag[i][j+1] = false;
             }
             //left
             if(j-1 >= 0 && board[i][j-1] == word[index] && flag[i][j-1] == false){
                flag[i][j-1] = true;
                if(search(board, word, i, j-1, index+1, flag)) return true;
                flag[i][j-1] = false;
             }
             return false;
        }
        bool exist(vector<vector<char> > &board, string word) {
            // Start typing your C/C++ solution below
            // DO NOT write int main() function
            rows = board.size();
            columns = board[0].size();
            len = word.size();
            if(len == 0) return true ;
            if(rows * columns < len) return false;
           
            for(int i = 0; i< rows ;i++)
                for(int j = 0; j< columns ; j++){    
                    if(board[i][j] == word[0])
                    {
                       vector<vector<bool>>  flag(rows, vector<bool>(columns, false)) ;
                        flag[i][j] = true;
                        if(search(board,word, i, j,1,flag)) return true;
                       // flag[i][j] = false;
                    }
                }
                   
            return false;
        }
    private:
        int rows;
        int columns;
        int len ;
    };

    未AC的奇怪代码

    class Solution {
    public:
       bool search(vector<vector<char> > &board, const string & word, int i, int j, int index)
        {
              if(index == len) return true;
              //up
              if(i-1 >=0 && board[i-1][j] == word[index] && flag[i-1][j] == false) { 
                 flag[i-1][j] = true;
                 if(search(board, word, i-1, j,index+1) )  return true;
                 flag[i-1][j] = false;
              }
              //down
              if(i+1 < rows && board[i+1][j] == word[index] && flag[i+1][j] == false){
                flag[i+1][j] = true;
                if(search(board, word, i+1, j, index+1)) return true;
                flag[i+1][j] = false;
              }
             //right
             if( j+1 < columns && board[i][j+1] == word[index] && flag[i][j+1] == false){
                flag[i][j+1] = true;
                if(search(board, word, i, j+1, index+1)) return true;
                flag[i][j+1] = false;
             }
             //left
             if(j-1 >= 0 && board[i][j-1] == word[index] && flag[i][j-1] == false){
                flag[i][j-1] = true;
                if(search(board, word, i, j-1, index+1)) return true;
                flag[i][j-1] = false;
             }
             return false;
        }
        bool exist(vector<vector<char> > &board, string word) {
            // Start typing your C/C++ solution below
            // DO NOT write int main() function
            rows = board.size();
            columns = board[0].size();
            len = word.size();
            if(len == 0) return true ;
            if(rows * columns < len) return false;
            flag.resize(rows, vector<bool>(columns, false)) ;
            for(int i = 0; i< rows ;i++)
                for(int j = 0; j< columns ; j++){    
                    if(board[i][j] == word[0])
                    {            
                        flag[i][j] = true;
                        if(search(board,word, i, j,1)) return true;
                        flag[i][j] = false;
                    }
                }
                   
            return false;
        }
    private:
        vector<vector<bool>>  flag;
        int rows;
        int columns;
        int len ;
    };
    View Code
  • 相关阅读:
    CodeForces 510C Fox And Names (拓扑排序)
    Codeforces 1153D Serval and Rooted Tree (简单树形DP)
    HDU 6437 Problem L.Videos (最大费用)【费用流】
    Luogu P3381 (模板题) 最小费用最大流
    Codeforces 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses (并查集+分组背包)
    Codeforces 1144F Graph Without Long Directed Paths (DFS染色+构造)
    HDU 2204 Eddy's 爱好 (容斥原理)
    Codeforces 939E Maximize! (三分 || 尺取)
    Codeforces 938D. Buy a Ticket (最短路+建图)
    CodeForces 959E Mahmoud and Ehab and the xor-MST (MST+找规律)
  • 原文地址:https://www.cnblogs.com/graph/p/3237065.html
Copyright © 2011-2022 走看看