[LeetCode 中等 数组]扫雷游戏 BFS DFS

题目描述

让我们一起来玩扫雷游戏!

给定一个代表游戏板的二维字符矩阵。 ‘M’ 代表一个未挖出的地雷,‘E’ 代表一个未挖出的空方块,‘B’ 代表没有相邻(上,下,左,右,和所有4个对角线)地雷的已挖出的空白方块,数字(‘1’ 到 ‘8’)表示有多少地雷与这块已挖出的方块相邻,‘X’ 则表示一个已挖出的地雷。

现在给出在所有未挖出的方块中(‘M’或者’E’)的下一个点击位置(行和列索引),根据以下规则,返回相应位置被点击后对应的面板:

如果一个地雷(‘M’)被挖出,游戏就结束了- 把它改为 ‘X’。
如果一个没有相邻地雷的空方块(‘E’)被挖出,修改它为(‘B’),并且所有和其相邻的未挖出方块都应该被递归地揭露。
如果一个至少与一个地雷相邻的空方块(‘E’)被挖出,修改它为数字(‘1’到’8’),表示相邻地雷的数量。
如果在此次点击中,若无更多方块可被揭露,则返回面板。

示例 1:

输入:

[[‘E’, ‘E’, ‘E’, ‘E’, ‘E’],
[‘E’, ‘E’, ‘M’, ‘E’, ‘E’],
[‘E’, ‘E’, ‘E’, ‘E’, ‘E’],
[‘E’, ‘E’, ‘E’, ‘E’, ‘E’]]

Click : [3,0]

输出:

[[‘B’, ‘1’, ‘E’, ‘1’, ‘B’],
[‘B’, ‘1’, ‘M’, ‘1’, ‘B’],
[‘B’, ‘1’, ‘1’, ‘1’, ‘B’],
[‘B’, ‘B’, ‘B’, ‘B’, ‘B’]]

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/minesweeper
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

DFS

class Solution {
    int[][] move = {{1,1},{1,-1},{-1,-1},{-1,1},{-1,0},{1,0},{0,-1},{0,1}};
    Queue<int[]> queue = new LinkedList<int[]>();
    boolean[][] visit;
    int directions = 8;

    public char[][] updateBoard(char[][] board, int[] click) {
        visit=new boolean[board.length][board[0].length];
        int x = click[0];
        int y = click[1];
        if(board[x][y]=='M'){
            board[x][y]='X';
        }else{
            bfs(board,x,y);
        }

        return board;
    }


    public void dfs(char[][] board,int x, int y){
        int count=0;
        for(int i=0;i<directions;i++){
            int newX=move[i][0]+x;
            int newY=move[i][1]+y;
            if(newX>=0&&newY>=0&&newX<board.length&&newY<board[0].length){
                 if(board[newX][newY] == 'M') count++;
            }
        }

        if(count==0){
           board[x][y] = 'B';
           for(int i=0;i<directions;i++) {
                int newX=move[i][0]+x;
                int newY=move[i][1]+y;
                if(newX>=0&&newY>=0&&newX<board.length&&newY<board[0].length&&board[newX][newY] == 'E'){
                    dfs(board,newX,newY);
                }    
           }
        }else{ 
            board[x][y] = (char)(count+ '0');
            }
    }  

BFS

    public void dfs(char[][] board,int x, int y){
        int count=0;
        for(int i=0;i<directions;i++){
            int newX=move[i][0]+x;
            int newY=move[i][1]+y;
            if(newX>=0&&newY>=0&&newX<board.length&&newY<board[0].length){
                 if(board[newX][newY] == 'M') count++;
            }
        }

        if(count==0){
           board[x][y] = 'B';
           for(int i=0;i<directions;i++) {
                int newX=move[i][0]+x;
                int newY=move[i][1]+y;
                if(newX>=0&&newY>=0&&newX<board.length&&newY<board[0].length&&board[newX][newY] == 'E'){
                    dfs(board,newX,newY);
                }    
           }
        }else{ 
            board[x][y] = (char)(count+ '0');
            }
    }

    public void bfs(char[][] board,int x,int y){
        
        visit[x][y]= true;
        queue.offer(new int[] {x,y});

        while(!queue.isEmpty()){
            int count=0;
            int[] cur = queue.poll();
            x=cur[0];
            y=cur[1];
           
            visit[x][y]= true;

            for(int i=0;i<directions;i++){
                int newX=move[i][0]+x;
                int newY=move[i][1]+y;
                if(newX>=0&&newY>=0&&newX<board.length&&newY<board[0].length){
                    if(board[newX][newY] == 'M') count++;
                }
            }

            if(count==0){
                board[cur[0]][cur[1]] = 'B';
                for(int i=0;i<directions;i++) {
                    int newX=move[i][0]+x;
                    int newY=move[i][1]+y;
                        if(newX>=0&&newY>=0&&newX<board.length&&newY<board[0].length&&board[newX][newY] == 'E'&& !visit[newX][newY]){
                            queue.offer(new int[] {newX,newY});
                        }    
                }
                }else{ 
                     board[cur[0]][cur[1]] = (char)(count+ '0');
                    }
            }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值