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

 

你可能感兴趣的:(LeetCode)