"Leetcode" Word Search (middle)

Source: Internet
Author: User

Starting today, the retrospective hardening phase.

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 is those horizontally or V Ertically neighboring. The same letter cell is used more than once.

For example,
Given board =

[  ["ABCE"],  ["SFCs"],  ["Adee"]]

Word = "ABCCED" , returns true ,
Word = "SEE" , returns true ,
Word = "ABCB" , returns false .

Ideas:

Because we already know it's backtracking, so just follow the steps.

AC, but the time is quite long 520ms

classSolution { Public:    BOOLexist (vector<vector<Char> > &board,stringword) {Vector<vector<int>>S (Word.length ()); Vector<int>X (Word.length ()); introw =board.size (); if(Row = =0)            returnWord.length () = =0; intCol = board[0].size (); //find the position of the first letter         for(inti =0; i < row; i++)        {             for(intj =0; J < Col; J + +)            {                if(Board[i][j] = = word[0]) s[0].push_back (i * col +j); }        }        intK =0; //Backtracking Solution         while(k >=0)        {             while(!S[k].empty ()) {                intLOC =S[k].back (); X[K]=Loc; intr = loc/Col; intc = loc%Col;                S[k].pop_back (); if(K < Word.length ()-1) {k= k +1; if(R-1>=0&& Board[r-1][C] = = Word[k])//above the                    {                        intLoctmp = (R-1) * col +C; if(!Isalreadyexist (X, K, loctmp))                    S[k].push_back (LOCTMP); }                    if(R +1< row && Board[r +1][C] = = Word[k])//the following                    {                        intLoctmp = (R +1) * col +C; if(!Isalreadyexist (X, K, loctmp))                    S[k].push_back (LOCTMP); }                    ifC1>=0&& Board[r][c-1] = = Word[k])//on the left.                    {                        intLoctmp = R * Col + C-1; if(!Isalreadyexist (X, K, loctmp))                    S[k].push_back (LOCTMP); }                    if(C +1< col && Board[r][c +1] = = Word[k])//on the right.                    {                        intLoctmp = R * Col + C +1; if(!Isalreadyexist (X, K, loctmp))                    S[k].push_back (LOCTMP); }                }                Else                {                    return true; }} k= K-1; }        return false; }    BOOLIsalreadyexist (vector<int> X,intKintLoc) {         for(inti =0; I < K; i++)        {            if(X[i] = =Loc)return true; }        return false; }};

The Great God 100ms Code, with recursion, in judging whether it has been used to directly find the table, will be much faster. Not find the first letter position, but unified view, the amount of code will be less

classSolution {Private: Vector<vector<Char> > *Board; string*Word; BOOL**used;Private:    BOOLIsinboard (intIintj) {if(I <0)return false; if(I >= board->size ())return false; if(J <0)return false; if(J >= (*board) [I].size ())return false; return true; }    BOOLDFS (intSiintSjintN) {if(n = = Word->size ())return true; if(Isinboard (SI, sj)) {if(!USED[SI][SJ] && (*board) [SI][SJ] = = (*word) [n]) {USED[SI][SJ]=true; BOOLRET =false; if(DFS (si+1, SJ, n+1)) RET=true; Else if(DFS (si-1, SJ, n+1)) RET=true; Else if(DFS (SI, sj+1, n+1)) RET=true; Else if(DFS (SI, sj-1, n+1)) RET=true; USED[SI][SJ]=false; returnret; }        }        return false; } Public:    BOOLexist (vector<vector<Char> > &board,stringword) {        if(board.size () = =0)return false;  This->board = &Board;  This->word = &Word; Used=New BOOL*[Board.size ()];  for(inti =0; I < board.size (); i + +) {Used[i]=New BOOL[Board[i].size ()];  for(intj =0; J < Board[i].size (); J + +) Used[i][j]=false; }         for(inti =0; I < board.size (); i + +)             for(intj =0; J < Board[i].size (); J + +)                if(DFS (I, J,0))return true; return false; }};

"Leetcode" Word Search (middle)

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.