leetcode 529. Minesweeper

257 篇文章 17 订阅

Let's play the minesweeper game (Wikipediaonline game)!

You are given a 2D char matrix representing the game board. 'M' represents an unrevealed mine, 'E' represents an unrevealed empty square, 'B' represents a revealed blank square that has no adjacent (above, below, left, right, and all 4 diagonals) mines, digit ('1' to '8') represents how many mines are adjacent to this revealed square, and finally 'X' represents a revealed mine.

Now given the next click position (row and column indices) among all the unrevealed squares ('M' or 'E'), return the board after revealing this position according to the following rules:

  1. If a mine ('M') is revealed, then the game is over - change it to 'X'.
  2. If an empty square ('E') with no adjacent mines is revealed, then change it to revealed blank ('B') and all of its adjacent unrevealed squares should be revealed recursively.
  3. If an empty square ('E') with at least one adjacent mine is revealed, then change it to a digit ('1' to '8') representing the number of adjacent mines.
  4. Return the board when no more squares will be revealed.

Example 1:

Input: 

[['E', 'E', 'E', 'E', 'E'],
 ['E', 'E', 'M', 'E', 'E'],
 ['E', 'E', 'E', 'E', 'E'],
 ['E', 'E', 'E', 'E', 'E']]

Click : [3,0]

Output: 

[['B', '1', 'E', '1', 'B'],
 ['B', '1', 'M', '1', 'B'],
 ['B', '1', '1', '1', 'B'],
 ['B', 'B', 'B', 'B', 'B']]

Explanation:

Example 2:

Input: 

[['B', '1', 'E', '1', 'B'],
 ['B', '1', 'M', '1', 'B'],
 ['B', '1', '1', '1', 'B'],
 ['B', 'B', 'B', 'B', 'B']]

Click : [1,2]

Output: 

[['B', '1', 'E', '1', 'B'],
 ['B', '1', 'X', '1', 'B'],
 ['B', '1', '1', '1', 'B'],
 ['B', 'B', 'B', 'B', 'B']]

Explanation:

Note:

  1. The range of the input matrix's height and width is [1,50].
  2. The click position will only be an unrevealed square ('M' or 'E'), which also means the input board contains at least one clickable square.
  3. The input board won't be a stage when game is over (some mines have been revealed).
  4. For simplicity, not mentioned rules should be ignored in this problem. For example, you don't need to reveal all the unrevealed mines when the game is over, consider any cases that you will win the game or flag any squares.

这一题是个搜索问题,就按照它的规则来做:
1. 如果点击到M,则标记它为X,停止后续的搜索。
2. 如果点击到空格子(E),则接下来如何,取决于它四周有多少个雷
    2.1 如果它的四周有雷,则用它周围雷的数目来标记它,停止后续的搜索
    2.2 如果它的四周没有雷,则用'B'标记它,对它的8个相邻格子进行后续的搜索

我的代码一如既往地很长-_-;|

public class Minesweeper_529 {

	public char[][] updateBoard(char[][] board, int[] click) {
		int x=click[0];
		int y=click[1];
		if(board[x][y]=='M'){
			board[x][y]='X';
			return board;
		}
		if(board[x][y]!='E'){
			return board;
		}
		int xLength=board.length;
		int yLength=board[0].length;
		int countMines=0;
		if(x-1>=0){//不在第一行
			if(board[x-1][y]=='M'){//[x-1][y]
				countMines++;
			}
			if(y-1>=0){
				if(board[x-1][y-1]=='M'){//[x-1][y-1]
					countMines++;
				}
			}
			if(y+1<yLength){
				if(board[x-1][y+1]=='M'){//[x-1][y+1]
					countMines++;
				}
			}
		}
		if(x+1<xLength){//不在最后一行
			if(board[x+1][y]=='M'){//[x+1][y]
				countMines++;
			}
			if(y-1>=0){
				if(board[x+1][y-1]=='M'){//[x+1][y-1]
					countMines++;
				}
			}
			if(y+1<yLength){
				if(board[x+1][y+1]=='M'){//[x+1][y+1]
					countMines++;
				}
			}
		}
		if(y-1>=0){//不在最左列
			if(board[x][y-1]=='M'){//[x][y-1]
				countMines++;
			}
		}
		if(y+1<yLength){//不在最右列
			if(board[x][y+1]=='M'){//[x][y+1]
				countMines++;
			}
		}
		if(countMines!=0){
			board[x][y]=(char)('0'+countMines);
		}
		else{
			board[x][y]='B';
			if(x-1>=0){//不在第一行
				click[0]=x-1;
				click[1]=y;
				updateBoard(board, click);
				if(y-1>=0){
					click[0]=x-1;
					click[1]=y-1;
					updateBoard(board, click);
				}
				if(y+1<yLength){
					click[0]=x-1;
					click[1]=y+1;
					updateBoard(board, click);
				}
			}
			if(x+1<xLength){//不在最后一行
				click[0]=x+1;
				click[1]=y;
				updateBoard(board, click);
				if(y-1>=0){
					click[0]=x+1;
					click[1]=y-1;
					updateBoard(board, click);
				}
				if(y+1<yLength){
					click[0]=x+1;
					click[1]=y+1;
					updateBoard(board, click);
				}
			}
			if(y-1>=0){//不在最左列
				click[0]=x;
				click[1]=y-1;
				updateBoard(board, click);
			}
			if(y+1<yLength){//不在最右列
				click[0]=x;
				click[1]=y+1;
				updateBoard(board, click);
			}
		}
		return board;
	}

	public static void main(String[] args) {
		Minesweeper_529 m=new Minesweeper_529();
		char[][] a=new char[][]{{'E', 'E', 'E', 'E', 'E'},{'E', 'E', 'M', 'E', 'E'},
			{'E', 'E', 'E', 'E', 'E'},{'E', 'E', 'E', 'E', 'E'}};
		int[] click=new int[]{3,0};
		char[][] b=m.updateBoard(a, click);
		int xLength=b.length;
		int yLength=b[0].length;
		for(int i=0;i<xLength;i++){
			for(int j=0;j<yLength;j++){
				System.out.print(b[i][j]+" ");
			}
			System.out.println();
		}
	}
}
我是用很多if else句子来遍历相邻的8个格子的,有大神提取出共同之处,写法更加简洁明了:
public class Solution {
    public char[][] updateBoard(char[][] board, int[] click) {
        int x = click[0], y = click[1];
        if (board[x][y] == 'M') {
            board[x][y] = 'X';
            return board;
        }
        
        dfs(board, x, y);
        return board;
    }
    
    int[] dx = {-1, 0, 1, -1, 1, 0, 1, -1};
    int[] dy = {-1, 1, 1, 0, -1, -1, 0, 1};
    private void dfs(char[][] board, int x, int y) {
        if (x < 0 || x >= board.length || y < 0 || y >= board[0].length || board[x][y] != 'E')  return;
        
        int num = getNumsOfBombs(board, x, y);
    
        if (num == 0) {
            board[x][y] = 'B';
            for (int i = 0; i < 8; i++) {
                int nx = x + dx[i], ny = y + dy[i];
                dfs(board, nx, ny);
            }
        } else {
            board[x][y] = (char)('0' + num);
        }
        
    }
    
    private int getNumsOfBombs(char[][] board, int x, int y) {
        int num = 0;
        for (int i = -1; i <= 1; i++) {
            for (int j = -1; j <= 1; j++) {
                int nx = x + i, ny = y + j;
                if (nx < 0 || nx >= board.length || ny < 0 || ny >= board[0].length)    continue;
                if (board[nx][ny] == 'M' || board[nx][ny] == 'X') {
                    num++;
                }
            }
        }
        return num;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值