原题链接在这里: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. 使用bootstrap时碰到问题$(...).modal is not a function

    我出现这个问题是,因为bootstrap没有正确引入. 除了bootstrap的路径需要被正确引入外,它引入时还要放在jquery.js后面,否则也会报这个错误.

  2. POJ1247-Magnificent Meatballs

    http://poj.org/problem?id=1247 Magnificent Meatballs Time Limit: 1000MS   Memory Limit: 10000K Total ...

  3. em和px的区别一次彻底搞清楚!

    在国内网站中,包括三大门户,以及“引领”中国网站设计潮流的蓝色理想,ChinaUI等都是使用了px作为字体单位.只有百度好歹做了个可调的表率.而 在大洋彼岸,几乎所有的主流站点都使用em作为字体单位, ...

  4. 微信小程序-----安装,编写第一个小程序和运行到手机端

    第一步: 微信公众平台注册账号,并选择小程序,网址:mp.weixin.qq.com 填写相关信息,如:主体类型(个人或者企业) AppID  在开发中都是用的到的,服务器域名在网络请求也是用的到的. ...

  5. Attribute 'items' must be an array, a Collection or a Map错误解决!

    唉!真的要说一句话叫做论一串代码的重要性!就是如此的气人!气的牙根痒痒! 前几天刚刚写过SpringMVC之ModelAndView的 jsp值在浏览页面不显示的问题!也是因为这一串代码,但是这一次一 ...

  6. IOS-实战分享:实时美颜滤镜是怎样炼成的

    作者:琨君 原文链接:http://www.jianshu.com/p/945fc806a9b4 本文获作者授权转载 背景 前段时间由于项目需求,做了一个基于GPUImage的实时美颜滤镜.现在各种各 ...

  7. section和div

    section和div 一.DIV div即division(区块),把文档分割为独立的.不同的部份.作用,以下三种情况应该用div标签: 1.用于页面布局,且不是 header.footer 之类的 ...

  8. Java复习3.变量.常量.String.

    Java 中的变量常量数据类型 20131004 前言: 还是国庆节,无聊的很,就没事复习点Java的知识,其实C/C++基本上是现在大型企业面试的语言,但是多学习点Java是没有坏处的,而且,将来工 ...

  9. 二十、dbms_stats(用于搜集,查看,修改数据库对象的优化统计信息)

    1.概述 作用:用于搜集,查看,修改数据库对象的优化统计信息. 2.包的组成 1).get_column_stats作用:用于取得列的统计信息语法:dbms_stats.get_column_stat ...

  10. 最大匹配算法 (Maximum Matching)

    之所以研究这个算法,是因为最近在研究NLP中文的分词,所谓分词就是将一个完整的句子,例如“计算语言学课程有意思”,分解成一些词组单元“计算语言学,课程,有,意思”. “最大匹配法” 在中文分词中有所应 ...