LeetCode 529. 소뇌놀이

1284 단어 LeetCodeDFS
원제목:https://leetcode-cn.com/problems/minesweeper/
 
아이디어:
ffs, 규칙에 주의하면 됩니다.
 
코드:
class Solution {
    int x[8] = {-1,-1,-1,0,0,1,1,1};
    int y[8] = {-1,0,1,-1,1,-1,0,1};
    void dfs(vector>& board, int p, int q){
        int count=0;
        for(int i = 0;i<8;i++){
            int tp = p+x[i],tq = q+y[i];
            if(tp<0 || tp>=board.size() ||tq<0||tq>=board[0].size()) continue;
            if(board[tp][tq] == 'M') ++count;
        }
        if(count == 0){
            board[p][q] = 'B';
            for(int i=0;i<8;i++){
                int tp = p+x[i],tq = q+y[i];
                if(tp<0 || tp>=board.size() ||tq<0||tq>=board[0].size()||board[tp][tq]!='E') continue;
                dfs(board,tp,tq);
            } 
        }
        else{
            board[p][q] = '0'+count;
        }
    }
public:
    vector> updateBoard(vector>& board, vector& click) {
        int m = board.size(),n = board[0].size();
        int p = click[0],q=click[1];
        if(board[p][q] == 'M') board[p][q] = 'X';
        else dfs(board,p,q);
        return board;
    }
};

좋은 웹페이지 즐겨찾기