给定一个代表游戏板的二维字符矩阵。'M' 代表一个未挖出的地雷,'E' 代表一个未挖出的空方块,'B' 代表没有相邻(上,下,左,右,和所有4个对角线)地雷的已挖出的空白方块,数字('1' 到 '8')表示有多少地雷与这块已挖出的方块相邻,'X' 则表示一个已挖出的地雷。
现在给出在所有未挖出的方块中('M'或者'E')的下一个点击位置(行和列索引),根据以下规则,返回相应位置被点击后对应的面板:
1.如果一个地雷('M')被挖出,游戏就结束了,把它改为 'X'。
2.如果一个没有相邻地雷的空方块('E')被挖出,修改它为('B'),并且所有和其相邻的未挖出方块都应该被递归地揭露。
3.如果一个至少与一个地雷相邻的空方块('E')被挖出,修改它为数字('1'到'8'),表示相邻地雷的数量。
4.如果在此次点击中,若无更多方块可被揭露,则返回面板。
示例 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']]
示例 2:
输入:
[['B', '1', 'E', '1', 'B'],
['B', '1', 'M', '1', 'B'],
['B', '1', '1', '1', 'B'],
['B', 'B', 'B', 'B', 'B']]
Click : [1,2]
输出:
[['B', '1', 'E', '1', 'B'],
['B', '1', 'X', '1', 'B'],
['B', '1', '1', '1', 'B'],
['B', 'B', 'B', 'B', 'B']]
注意:
输入矩阵的宽和高的范围为 [1,50]。
点击的位置只能是未被挖出的方块 ('M' 或者 'E'),这也意味着面板至少包含一个可点击的方块。
输入面板不会是游戏结束的状态(即有地雷已被挖出)。
简单起见,未提及的规则在这个问题中可被忽略。例如,当游戏结束时你不需要挖出所有地雷,考虑所有你可能赢得游戏或标记方块的情况。
当前点击的是「未挖出的地雷」,我们将其值改为 X 即可。
当前点击的是「未挖出的空方块」,我们需要统计它周围相邻的方块里地雷的数量 count(即 M 的数量)。如果 count 为零,即执行规则 2,此时需要将其改为 B,且递归地处理周围的八个未挖出的方块,递归终止条件即为规则 4,没有更多方块可被揭露的时候。否则执行规则 3,将其修改为数字即可
package com.loo;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
public class MineSweeperOneStep {
public static int[] dirX = new int[] {0 , 1 , 0 , -1 , 1 , 1 , -1 , -1};
public static int[] dirY = new int[] {1 , 0 , -1 , 0 , 1 , -1 , 1 , -1};
public static void main(String[] args) {
char[][] board1 = new char[][] {
{'E', 'E', 'E', 'E', 'E'},
{'E', 'E', 'M', 'E', 'E'},
{'E', 'E', 'E', 'E', 'E'},
{'E', 'E', 'E', 'E', 'E'}
};
int[] step1 = new int[]{3 , 0};
char[][] oneStep1 = oneStep(board1 , step1);
for (char[] b : oneStep1) {
System.out.println(Arrays.toString(b));
}
System.out.println("-------------------");
char[][] board2 = new char[][] {
{'B', '1', 'E', '1', 'B'},
{'B', '1', 'M', '1', 'B'},
{'B', '1', '1', '1', 'B'},
{'B', 'B', 'B', 'B', 'B'}
};
int[] step2 = new int[]{1 , 2};
char[][] oneStep2 = oneStep(board2 , step2);
for (char[] b : oneStep2) {
System.out.println(Arrays.toString(b));
}
}
public static char[][] oneStep(char[][] board , int[] step) {
int x = step[0];
int y = step[1];
if (board[x][y] == 'M') { // 规则 1
board[x][y] = 'X';
} else {
dfs(board , x , y);
// bfs(board , x , y);
}
return board;
}
public static void dfs(char[][] board , int x , int y) {
int count = 0;
for (int i=0;i<8;i++) {
int dx = x + dirX[i];
int dy = y + dirY[i];
if (dx<0 || dx>=board.length || dy<0 || dy>=board[0].length) {
continue;
}
if (board[dx][dy] == 'M') {
count ++;
}
}
if (count>0) { // 规则 3
board[x][y] = (char)(count + '0');
} else { // 规则 2
board[x][y] = 'B';
for (int i=0;i<8;i++) {
int dx = x + dirX[i];
int dy = y + dirY[i];
if (dx<0 || dx>=board.length || dy<0 || dy>=board[0].length || board[dx][dy] != 'E') {
continue;
}
dfs(board , dx , dy);
}
}
}
public static void bfs(char[][] board , int x , int y) {
Queue<int[]> queue = new LinkedList<int[]>();
boolean[][] visit = new boolean[board.length][board[0].length];
queue.offer(new int[] {x , y});
visit[x][y] = true;
while (!queue.isEmpty()) {
int[] pos = queue.poll();
int dx = pos[0];
int dy = pos[1];
int count = 0;
for (int i=0;i<8;i++) {
int tx = dx + dirX[i];
int ty = dy + dirY[i];
if (tx<0 || tx>=board.length || ty<0 || ty>=board[0].length) {
continue;
}
if (board[tx][ty] == 'M') {
count ++;
}
}
if (count>0) {
board[dx][dy] = (char)(count + '0');
} else {
board[dx][dy] = 'B';
for (int i=0;i<8;i++) {
int tx = dx + dirX[i];
int ty = dy + dirY[i];
if (tx<0 || tx>=board.length || ty<0 || ty>=board[0].length || board[tx][ty] != 'E' || visit[tx][ty]) {
continue;
}
visit[tx][ty] = true;
queue.offer(new int[] {tx , ty});
}
}
}
}
}