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 ...
随机推荐
- jQuery EasyUI简介
jQuery EasyUI 是一个基于 jQuery 的框架,集成了各种用户界面插件. jQuery EasyUI是一组基于jQuery的UI插件集合体,而jQuery EasyUI的目标就是帮助we ...
- Spring Cloud(一)服务的注册与发现(Eureka)
Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中涉及的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全局锁.决策竞选.分布式会话和集 ...
- RobHess的SIFT代码解析之kd树
平台:win10 x64 +VS 2015专业版 +opencv-2.4.11 + gtk_-bundle_2.24.10_win32 主要参考:1.代码:RobHess的SIFT源码:SIFT+KD ...
- RT-Thread代码启动过程与$Sub$ $main、$Super$ $main
文章转载自:https://blog.csdn.net/yang1111111112/article/details/80913001 我们找到系统复位的地方,可以往下单步跟踪. ①从系统初始化开始执 ...
- Centos7.4安装RabbitMQ
1.1 安装RabbitMQ 1.1.1 系统环境 [root@rabbitmq ~]# cat /etc/redhat-release CentOS Linux release 7.4.1708 ( ...
- (a ==1 && a== 2 && a==3) 有可能是 true 吗?
今天看到这个题目,感到很有意思,查找了些资料,各位牛人果然有高招解决: 方法一: var a = { i: 1, toString: function () { return a.i++; } } i ...
- C#的Lazy与LazyInitializer
class Program { static void Main(string[] args) { //初始化 Lazy 类的新实例 //当延迟初始化发生时,将使用指定的初始化函数和初始化模式 // ...
- AGC刷题记
已经刷不了几天了... AGC001 A-BBQ Easy 排个序就过了 B-Mysterious Light 手膜一下,你会发现魔改一下\(gcd\)就行了 C-Shorten Diameter 刚 ...
- zxy
ZXY标准瓦片 发送反馈 SuperMap iServer .iEdge支持读取ZXY规范的地图瓦片,可对接 OpenStreetMap 等互联网的瓦地图服务.ZXY规范的地图瓦片规则如下:将地图全幅 ...
- flutter 项目中打印原生安卓的log信息
因为项目的需要 在flutter 中调用安卓的方法 再用安卓的方法去调用c写的so包 方法 如果当前项目下面没有android stduio 自带的logcat 那就利用下面的方法 在安卓代码中引入 ...