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 ...
随机推荐
- mvn deploy返回400错误的几种可能
user credentials are wrong url to server is wrong user does not have access to the deployment reposi ...
- Git常用命令和Git团队使用规范指南
转自:https://wsgzao.github.io/post/git/ 前言 在2005年的某一天,Linux之父Linus Torvalds 发布了他的又一个里程碑作品——Git.它的出现改变了 ...
- [oracle原]访问局域网内出现“ORA-12541:TNS:无监听程序”
近日在服务器局域网内27电脑上安装了oracle11g,本机上访问此数据库正常.但在局域网内其它机器上访问27上的数据库时,出现“ORA-12541:TNS:无监听程序”错误. 查27上的配置:D:\ ...
- 二十二 Python分布式爬虫打造搜索引擎Scrapy精讲—scrapy模拟登陆和知乎倒立文字验证码识别
第一步.首先下载,大神者也的倒立文字验证码识别程序 下载地址:https://github.com/muchrooms/zheye 注意:此程序依赖以下模块包 Keras==2.0.1 Pillow= ...
- docker nginx 问题
'经常不启动docker会遇到如下问题 启动docker pull * 会报错 1. 安装步骤: 解决办法:命令输入:docker logout 再次执行:docker pull * 2. 执行ru ...
- 【转】线程池体系介绍及从阿里Java开发手册学习线程池的正确创建方法
jdk1.7中java.util.concurrent.Executor线程池体系介绍 java.util.concurrent.Executor : 负责线程的使用与调度的根接口 |–Execut ...
- Idea_02_常用配置
一.前言 在上一节,我们安装并激活了IDEA,这一节我们来设置下Idea的常用配置: 项目相关配置 Idea常用配置 二.项目相关配置 运行Idea,出现下图 1.配置默认JDK 1.1 添加 SDK ...
- 修复 海盗船 k70 lux 未检测到设备(k70 no device detected)
corsair k70 lux 上周收到的生日礼物,头一次用机械键盘,还是这么高端的机械键盘(729RMB),手感一级棒.但是,有问题啊!把键盘上的 bios按钮拨到8上电脑可以识别,scroll 灯 ...
- c# html内容处理类
using System; using System.Text; using System.Text.RegularExpressions; using System.Net; using Syste ...
- Vim技能修炼教程(13) - 变量
VimScript变量 上节我们介绍了Python和Ruby来编写Vim插件的方式. 不过,Python和Ruby并不是所有的Vim都支持的功能,如果以最小依赖的原则来说,还是原汁原味的Vimscri ...