原题链接在这里:https://leetcode.com/problems/minesweeper/description/

题目:

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.

题解:

可以采用BFS, 最开始的click, if it is B, add it to the que.

For the poll position, for all 8 neighbors, if it is E and could become B, add to the que.

Note: it is 8 neighbors, not only 4.

Time Complexity: O(m*n). m = board.length. n = board[0].length.

Space: O(m*n).

AC Java:

 class Solution {
public char[][] updateBoard(char[][] board, int[] click) {
if(board == null || board.length == 0 || board[0].length == 0 || click == null || click.length != 2){
return board;
} int m = board.length;
int n = board[0].length;
if(click[0] < 0 || click[0] >= m || click[1] < 0 || click[1] >= n){
return board;
} LinkedList<int []> que = new LinkedList<>();
if(board[click[0]][click[1]] == 'M'){
board[click[0]][click[1]] = 'X';
return board;
} int count = getCount(board, click);
if(count > 0){
board[click[0]][click[1]] = (char)('0' + count);
return board;
} int [][] dirs = new int[][]{{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
board[click[0]][click[1]] = 'B';
que.add(click);
while(!que.isEmpty()){
int [] cur = que.poll();
for(int i = -1; i <= 1; i++){
for(int j = -1; j <=1; j++){
if(i == 0 && j == 0){
continue;
} int x = cur[0] + i;
int y = cur[1] + j;
if(x < 0 || x >= m || y < 0 || y >= n || board[x][y] != 'E'){
continue;
} int soundCount = getCount(board, new int[]{x, y});
if(soundCount > 0){
board[x][y] = (char)('0' + soundCount);
}else{
board[x][y] = 'B';
que.add(new int[]{x, y});
}
}
}
} return board;
} private int getCount(char [][] board, int [] arr){
int count = 0; for(int i = -1; i <= 1; i++){
for(int j = -1; j <= 1; j++){
if(i == 0 && j == 0){
continue;
} int x = arr[0] + i;
int y = arr[1] + j;
if(x < 0 || x >= board.length || y < 0 || y >= board[0].length){
continue;
} if(board[x][y] == 'M' || board[x][y] == 'X'){
count++;
}
}
} return count;
}
}

也可以采用DFS. DFS state needs the board and current click index.

Time Complexity: O(m*n). m = board.length, n = board[0].length.

Space: O(m*n).

AC Java:

 class Solution {
public char[][] updateBoard(char[][] board, int[] click) {
if(board == null || board.length == 0 || board[0].length == 0){
return board;
} int m = board.length;
int n = board[0].length; int r = click[0];
int c = click[1];
if(board[r][c] == 'M'){
board[r][c] = 'X';
}else{
board[r][c] = 'B'; int count = 0;
for(int i = -1; i<=1; i++){
for(int j = -1; j<=1; j++){
if(i==0 && j==0){
continue;
} int x = r+i;
int y = c+j;
if(x<0 || x>=m || y<0 || y>=n){
continue;
}
if(board[x][y] == 'M' || board[x][y] == 'X'){
count++;
}
}
} if(count > 0){
board[r][c] = (char)('0'+count);
}else{
for(int i = -1; i<=1; i++){
for(int j = -1; j<=1; j++){
if(i==0 && j==0){
continue;
} int x = r+i;
int y = c+j;
if(x<0 || x>=m || y<0 || y>=n){
continue;
}
if(board[x][y] == 'E'){
updateBoard(board, new int[]{x, y});
}
}
}
}
} return board;
}
}

LeetCode 529. Minesweeper的更多相关文章

  1. LN : leetcode 529 Minesweeper

    lc 529 Minesweeper 529 Minesweeper Let's play the minesweeper game! You are given a 2D char matrix r ...

  2. [LeetCode] 529. Minesweeper 扫雷

    Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix representin ...

  3. Week 5 - 529.Minesweeper

    529.Minesweeper Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char ma ...

  4. leetcode笔记(七)529. Minesweeper

    题目描述 Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix repres ...

  5. 【LeetCode】529. Minesweeper 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetco ...

  6. 529 Minesweeper 扫雷游戏

    详见:https://leetcode.com/problems/minesweeper/description/ C++: class Solution { public: vector<ve ...

  7. 529. Minesweeper扫雷游戏

    [抄题]: Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix repre ...

  8. [LeetCode] 529. Minesweeper_ Medium_ tag: BFS

    Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix representin ...

  9. LC 529. Minesweeper

    Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix representin ...

随机推荐

  1. [Vue]组件——通过$emit为组件自定义事件

    1.在定义组件时调用内建的 $emit 方法并传入事件的名字,来向父级组件触发一个事件enlarge-text: Vue.component('blog-post', { props: ['post' ...

  2. django 使用form组件提交数据之form表单提交

    django的form组件可以减少后台在进行一些重复性的验证工作,极大降低开发效率. 最近遇到一个问题: 当使用form表单提交数据后,如果数据格式不符合后台定义的规则,需要重新在前端页面填写数据. ...

  3. oracle 11g各种下载地址

    Oracle Database 11g Release 2 Standard Edition and Enterprise Edition Software Downloadsoracle 数据库 1 ...

  4. UVA-10972 RevolC FaeLoN (边双连通+缩点)

    题目大意:将n个点,m条边的无向图变成强连通图,最少需要加几条有向边. 题目分析:所谓强连通,就是无向图中任意两点可互达.找出所有的边连通分量,每一个边连通分量都是强连通的,那么缩点得到bcc图,只需 ...

  5. su | sudo su | sudo -i

    su <user> <user> <user> 需要输入user的密码,该命令改变user id,执行过后,以<user>中定义的用户运行shell,就 ...

  6. proxy-target-class 作用

    该属性值默认为false,表示使用JDK动态代理织入增强;当值为true时,表示使用CGLib动态代理织入增强;但是,即使设置为false,如果目标类没有生命接口, 则Spring将自动使用CGLib ...

  7. 【Windows】Python脚本随机启动

    Python脚本的管理在linux系统上市非常方便的,在windows则不是很方面.但是由于之前对于Windows这块的内容不是很了解,其实计划任务也是不错的,但和linux相比起来还是欠缺了那么点. ...

  8. python进行linux系统监控

      python进行linux系统监控 Linux系统下: 静态指标信息: 名称 描述 单位 所在文件 mem_total 内存总容量 KB /proc/meminfo disks 磁盘相关信息 - ...

  9. linux---nginx服务nfs服务nginx反向代理三台web

    一:nginx服务 1.二进制安装nginx包 [root@bogon ~]# systemctl disable firewalld #关闭Firewalls自启动 Removed symlink ...

  10. linux下网卡配置vlan

      yum install vconfig -y  modprobe 8021qvconfig add eth0 900 ifconfig eth0.900 172.16.90.57/24 up    ...