LeetCode 529. Minesweeper
原题链接在这里:https://leetcode.com/problems/minesweeper/description/
题目:
Let's play the minesweeper game (Wikipedia, online 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:
- If a mine ('M') is revealed, then the game is over - change it to 'X'.
- 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.
- 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.
- 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:
- The range of the input matrix's height and width is [1,50].
- The click position will only be an unrevealed square ('M' or 'E'), which also means the input board contains at least one clickable square.
- The input board won't be a stage when game is over (some mines have been revealed).
- 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的更多相关文章
- LN : leetcode 529 Minesweeper
lc 529 Minesweeper 529 Minesweeper Let's play the minesweeper game! You are given a 2D char matrix r ...
- [LeetCode] 529. Minesweeper 扫雷
Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix representin ...
- Week 5 - 529.Minesweeper
529.Minesweeper Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char ma ...
- leetcode笔记(七)529. Minesweeper
题目描述 Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix repres ...
- 【LeetCode】529. Minesweeper 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetco ...
- 529 Minesweeper 扫雷游戏
详见:https://leetcode.com/problems/minesweeper/description/ C++: class Solution { public: vector<ve ...
- 529. Minesweeper扫雷游戏
[抄题]: Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix repre ...
- [LeetCode] 529. Minesweeper_ Medium_ tag: BFS
Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix representin ...
- LC 529. Minesweeper
Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix representin ...
随机推荐
- angular-cli 工程中使用scss文件
angular/cli支持使用sass 新建工程: 如果是新建一个angular工程采用sass: ng new My_New_Project --style=sass 这样所有样式的地方都将采用sa ...
- 初识 JVM
发展历史 1996年,SUN JDK 1.0 Classic VM 发布,纯解释运行,使用外挂进行JIT 1997年 JDK1.1 发布.包含了:AWT.内部类.JDBC.RMI.反射 1998年 J ...
- 聚类效果评测-Fmeasure和Accuracy及其Matlab实现
聚类结果的好坏,有很多种指标,其中F-Measue即F值是常用的一种,其中包括precision(查准率或者准确率)和recall(查全率或者召回率). F-Measue是信息检索中常用的评价标准. ...
- web页面中 将几个字段post提交
思路 自己在html中构建form 先根据传入的action构建form的action 然后根据要提交的字段构建form中的元素 最后通过调用form中的按钮提交from表单 方法:var jsP ...
- Spring框架中,在工具类或者普通Java类中调用service或dao
spring注解的作用: 1.spring作用在类上的注解有@Component.@Responsity.@Service以及@Controller:而@Autowired和@Resource是用来修 ...
- zoj4028 LIS,差分约束
题意:给你以i为结尾的最长上升子序列的值,和每个值的区间范围求可行的a[i] 题解:差分约束,首先满足l[i]<=a[i]<=r[i],可以建一个虚拟节点n+1,那么有a[n+1]-a[i ...
- Codeforces Round #378 (Div. 2)F - Drivers Dissatisfaction GNU
http://codeforces.com/contest/733/problem/F 题意:给你一些城市和一些路,每条路有不满意程度和每减少一点不满意程度的花费,给出最大花费,要求找出花费小于s的最 ...
- oracle实例内存(SGA和PGA)调整-xin
一.名词解释 (1)SGA:System Global Area是Oracle Instance的基本组成部分,在实例启动时分配;系统全局域SGA主要由三部分构成:共享池.数据缓冲区.日志缓冲区. ( ...
- 快速切题 poj 1002 487-3279 按规则处理 模拟 难度:0
487-3279 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 247781 Accepted: 44015 Descr ...
- SpringBoot全家桶
前言 Spring简化了Java的开发,而SpringBoot简化了Spring.本文用SpringBoot采用分层的结构整合了filter,aspect,mybaits,logback,redis, ...