Leetcode: Word search

Source: Internet
Author: User

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,
GivenBoard=

[  ["ABCE"],  ["SFCS"],  ["ADEE"]]

Word="ABCCED",-> Returnstrue,
Word="SEE",-> Returnstrue,
Word="ABCB",-> Returnsfalse.

Address: https://oj.leetcode.com/problems/word-search/

Algorithm: Use the DFS plus backtracing method. First, start searching from every position on the board. Assume that the current traversal is to the position (I, j). If the first character of word is equal to that of the Board [I] [J], set the traversal position to true, and then call the function existcore to perform DFS. The last three parameters of the existcore function are represented. Currently, the traversal is to the position (I, j, and wait for the second POS character of the word to match. In the existcore function, if the last word of the word has been matched, the match is completed. Otherwise, the traversal starts from the (I, j) position and goes up to the left and right positions, if conditions are met, existcore is recursively called. Note: If the solution cannot be found in a certain direction, the corresponding flag position should be cleared and traced back to the original position. Code:

 1 class Solution { 2 public: 3     bool exist(vector<vector<char> > &board, string word) { 4         if(board.empty())   return false; 5         if(word.empty())    return true; 6         int m = board.size(); 7         vector<vector<bool> > flag; 8         for(int i = 0; i < m; ++i){ 9             int n = board[i].size();10             flag.push_back(vector<bool>(n,false));11         }12         for(int i = 0; i < m; ++i){13             int n = board[i].size();14             for(int j = 0; j < n; ++j){15                 if(word[0] == board[i][j]){16                     flag[i][j] = true;17                     if(existCore(board,word,flag,i,j,1))18                         return true;19                     flag[i][j] = false;20                 }21             }22         }23         return false;24     }25     bool existCore(vector<vector<char> > &board, string &word, vector<vector<bool> > &flag, int i, int j, int pos){26         if(pos == word.size()){27             return true;28         }29         if(i > 0 && j < board[i-1].size() && !flag[i-1][j] && board[i-1][j] == word[pos]){30             flag[i-1][j] = true;31             if(existCore(board,word,flag,i-1,j,pos+1)){32                 return true;33             }34             flag[i-1][j] = false;35         }36         if(j < board[i].size() - 1 && !flag[i][j+1] && board[i][j+1] == word[pos]){37             flag[i][j+1] = true;38             if(existCore(board,word,flag,i,j+1,pos+1)){39                 return true;40             }41             flag[i][j+1] = false;42         }43         if(i < board.size() - 1 && j < board[i+1].size() && !flag[i+1][j] && board[i+1][j] == word[pos]){44             flag[i+1][j] = true;45             if(existCore(board,word,flag,i+1,j,pos+1)){46                 return true;47             }48             flag[i+1][j] = false;49         }50         if(j > 0 && !flag[i][j-1] && board[i][j-1] == word[pos]){51             flag[i][j-1] = true;52             if(existCore(board,word,flag,i,j-1,pos+1)){53                 return true;54             }55             flag[i][j-1] = false;56         }57         return false;58     }59 };

 

Leetcode: Word search

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.