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
  • 相关阅读:
    .NET基础篇——利用泛型与反射更新实体(ADO.NET Entity Framework)(转)
    反射奥秘
    ubuntu 安装chmsee
    ubuntu安装sublime,新立得
    Ubuntu 升级VisualBox后无法启动 Kernel driver not installed
    ubuntu安装stardict并导入词典
    PHP运行模式
    Ubuntu ibus输入法图标消失的解决办法
    MySQL 获得当前日期时间(以及时间的转换)
    mysql索引之or条件
  • 原文地址:https://www.cnblogs.com/graph/p/3237065.html
Copyright © 2011-2022 走看看