原题链接在这里: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. uva11183最小树形图

    本来看数据用临界矩阵可能会超时,还是写了临界矩阵,结果1A了 模板的不能再模板 了 #include<map> #include<set> #include<cmath& ...

  2. 1-25-循环控制符break、continue和函数详解

    大纲: 1-for循环补充 1-1-for循环实战---类C格式应用 2-break.continue循环控制符 2-1实战:帮助理解break.continue作用 3-函数详解 3-1.脚本文件中 ...

  3. torch7 调用caffe model 作为pretrain

    torch7 调用caffe model 作为pretrain torch7 caffe preTrain model zoo torch7 通过 loadcaffe 包,可以调用caffe训练得到的 ...

  4. Ubuntu下彻底卸载mysql

    删除 mysql sudo apt-get autoremove --purge mysql-server-5.0 sudo apt-get remove mysql-server sudo apt- ...

  5. SQL HAVING用法详解

    来自:http://blog.csdn.net/wozeze1/article/details/6031318 HAVING 子句对 GROUP BY 子句设置条件的方式与 WHERE 和 SELEC ...

  6. Singleton单例类模式

    body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...

  7. IOC与DI简介

    IOC:控制反转(Inverse Of Control) 在没用spring框架之前我们是这样写程序的: private UserDao userDao = new UserDaoImpl(); 也就 ...

  8. arch初认识

    在这里下载最新的iso镜像,一个iso里面已经包括了32和64位. 来者不善. 进去后竟然是进入字符界面,并且给各位留了一个install.txt,果然..==! 好吧,粗略看下install.txt ...

  9. mac下解决mysql乱码问题

    问题描述:在window平台下面数据库插入.已经查找都是很正常的,但是到mac下面查找.插入就不正常了,之后感觉是mysql的问题然后网上搜索学习了下,果然是mysql的问题.解决方案:首先你要先去看 ...

  10. 添加机构organizations模块

    startapp organizations models内容: from django.db import models from datetime import datetime # Create ...