美文网首页
34. Word Search FROM Leetcode

34. Word Search FROM Leetcode

作者: 时光杂货店 | 来源:发表于2017-03-17 19:39 被阅读0次

    题目

    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 =

    [
    ['A','B','C','E'],
    ['S','F','C','S'],
    ['A','D','E','E']
    ]

    word = "ABCCED", -> returns true,
    word = "SEE", -> returns true,
    word = "ABCB", -> returns false.

    频度: 4

    解题之法

    class Solution {
    private:
        bool dfs(vector<vector<char>>& board, int row, int col, const string &word, int start, int M, int N, int sLen)
        {
            char curC;
            bool res = false;
            if( (curC = board[row][col]) != word[start]) return false;
            if(start==sLen-1) return true;
            board[row][col] = '*';
            if(row>0) res = dfs(board, row-1, col, word, start+1, M, N, sLen);
            if(!res && row < M-1) res = dfs(board, row+1, col, word, start+1, M, N, sLen);
            if(!res && col > 0)   res = dfs(board, row, col-1, word, start+1, M, N, sLen);
            if(!res && col < N-1) res = dfs(board,  row, col+1, word, start+1, M, N, sLen);
            board[row][col] = curC;
            return res;
        }
        
    public:
        bool exist(vector<vector<char>>& board, string word) {
            int M,N,i,j,sLen = word.size();
            if( (M=board.size()) && (N=board[0].size()) && sLen)
            {
                for(i=0; i<M; ++i)
                    for(j=0; j<N; ++j)
                        if(dfs(board, i, j, word, 0, M, N, sLen)) return true;
            }
            return false;
        }
    };
    

    相关文章

      网友评论

          本文标题:34. Word Search FROM Leetcode

          本文链接:https://www.haomeiwen.com/subject/bphynttx.html