[leetcode] Word Search

1853 단어
Word Search
역추적하여 방문한 문자는 "#", DFS로 수정됩니다.
class Solution {
public:
    bool exist(vector<vector<char> > &board, string word) {
        
        if (word.size()==0) {
            return true;// true
        }
        int row=board.size();
        int col=board[0].size();
        
        for (int i=0; i<row; i++) {
            for (int j=0; j<col; j++) {
                if (board[i][j]==word[0]) {
                    board[i][j]='#';//# 
                    if (dfs(board,i,j,word.substr(1))) {
                        return true;
                    }
                    board[i][j]=word[0];
                }
            }
        }
        
        return false;
    }
    
    bool dfs(vector<vector<char>> &board,int x,int y,string word ){
        if (word.size()==0) {
            return true;
        }
        
        bool flag=false;// 
        if (!flag&&x-1>=0&&board[x-1][y]==word[0]) {// 
            board[x-1][y]='#';
            flag=dfs(board, x-1, y, word.substr(1));
            board[x-1][y]=word[0];// 
        }
        if (!flag&&y-1>=0&&board[x][y-1]==word[0]) {
            board[x][y-1]='#';
            flag=dfs(board, x, y-1, word.substr(1));
            board[x][y-1]=word[0];
        }
        if (!flag&&x+1<board.size()&&board[x+1][y]==word[0]) {// 
            board[x+1][y]='#';
            flag=dfs(board, x+1, y, word.substr(1));
            board[x+1][y]=word[0];
        }
        if (!flag&&y+1<board[0].size()&&board[x][y+1]==word[0]) {// 
            board[x][y+1]='#';
            flag=dfs(board, x, y+1, word.substr(1));
            board[x][y+1]=word[0];
        }
        return flag;
    }
    
};

좋은 웹페이지 즐겨찾기