LC 529. Minesweeper
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.
Runtime: 28 ms, faster than 84.38% of C++ online submissions for Minesweeper.
class Solution {
public:
vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click) {
deque<pair<int,int>> q({ {click[], click[]} });
while(!q.empty()){
auto c = q.front().first, r = q.front().second, mines = ;
vector<pair<int,int>> neighbours;
if(board[c][r] == 'M') board[c][r] = 'X';
else for(int i=-; i<=; i++){
for(int j=-; j<=; j++){
if(c+i >= && r+j >= && c+i < board.size() && r+j < board[].size()){
if(board[c+i][r+j] == 'M') ++mines;
else if(mines == && board[c+i][r+j] == 'E') neighbours.push_back({c+i,r+j});
}
}
}
if(mines > ) board[c][r] = '' + mines;
else for(auto n : neighbours) {
board[n.first][n.second] = 'B';
q.push_back(n);
}
q.pop_front();
}
return board;
}
};
LC 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 ...
- Week 5 - 529.Minesweeper
529.Minesweeper Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char ma ...
- 529. Minesweeper扫雷游戏
[抄题]: Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix repre ...
- LeetCode 529. Minesweeper
原题链接在这里:https://leetcode.com/problems/minesweeper/description/ 题目: Let's play the minesweeper game ( ...
- leetcode笔记(七)529. Minesweeper
题目描述 Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix repres ...
- 529 Minesweeper 扫雷游戏
详见:https://leetcode.com/problems/minesweeper/description/ C++: class Solution { public: vector<ve ...
- [LeetCode] 529. Minesweeper 扫雷
Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix representin ...
- 【LeetCode】529. Minesweeper 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetco ...
- 529. Minesweeper
▶ 扫雷的扩展判定.已知棋盘上所有点的情况(雷区 'M',已翻开空白区 'B',未翻开空白区 'E',数字区 '1' ~ '8'),现在给定一个点击位置(一定在空白区域),若命中雷区则将被命中的 M ...
随机推荐
- Java虚拟机(JVM)知多少
本文大量参考:https://www.cnblogs.com/lfs2640666960/p/9297176.html 概述 JVM是JRE的一部分.它是一个虚构出来的计算机,是通过在实际的计算机上仿 ...
- JavaScript 的基本使用
JavaScript 基本语法要求: 1.JS的写法是严格区分大小写的. 2.标识符的起名要求跟java的是一样的,第一个位置可以说字母.下划线.美元符号.其他位置可以字母.下划线.美元符号.数字. ...
- Dart的List比较特殊的几个API
List里面常用的属性和方法: 常用属性: length 长度 reversed 翻转 isEmpty 是否为空 isNotEmpty 是否不为空 常用方法: add 增加 addAll 拼接数组 i ...
- C# Winform TabControl 双击关闭TabPage
在使用TabControl控件时,需要考虑自动创建的TabPage页实现——关闭功能 思路一:自定义TabControl控件,添加关闭按钮 思路二:TabControl控件TabPage显示ToolT ...
- 菜单项(Menu)的初步认识 以及 多级菜单(SubMenu)的初步认识
MainActivity.class public class MainActivity extends AppCompatActivity { private TextView textView; ...
- 10_Hive自定义函数UDF
Hive官方的UDF手册地址是:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF 1.使用内置函数的快捷方法: 创 ...
- springMVC的简单了解和环境搭建
一,什么mvc 模型-视图-控制器(MVC)是一个众所周知的以设计界面应用程序为基础的设计思想.它主要通过 分离模型.视图及控制器在应用程序中的角色 将业务逻辑从界面中解耦.通常, 模型负责封装应用程 ...
- linux下top命令的使用
top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器 视图参数含义 top视图分为两部分:操作系统资源概况信息和进程信息.首先分析资源 ...
- log4j2 日志打两遍的问题
在使用log4j2的时候,一般都需要不同的日志分类打印不同的日志等级,如下面的配置 <!-- 用于指定log4j自动重新配置的监测间隔时间,单位是秒 --> <configurati ...
- python之global用法
如果需要在函数内部改变函数外部的变量,就可以通过在函数内部声明变量为global变量.这样当程序运行至global变量便会替换外部的同名变量. 例1: # -*- coding:utf-8 -*- n ...