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.

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的更多相关文章

  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. Week 5 - 529.Minesweeper

    529.Minesweeper Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char ma ...

  3. 529. Minesweeper扫雷游戏

    [抄题]: Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix repre ...

  4. LeetCode 529. Minesweeper

    原题链接在这里:https://leetcode.com/problems/minesweeper/description/ 题目: Let's play the minesweeper game ( ...

  5. leetcode笔记(七)529. Minesweeper

    题目描述 Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix repres ...

  6. 529 Minesweeper 扫雷游戏

    详见:https://leetcode.com/problems/minesweeper/description/ C++: class Solution { public: vector<ve ...

  7. [LeetCode] 529. Minesweeper 扫雷

    Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix representin ...

  8. 【LeetCode】529. Minesweeper 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetco ...

  9. 529. Minesweeper

    ▶ 扫雷的扩展判定.已知棋盘上所有点的情况(雷区 'M',已翻开空白区 'B',未翻开空白区 'E',数字区 '1' ~ '8'),现在给定一个点击位置(一定在空白区域),若命中雷区则将被命中的 M ...

随机推荐

  1. 根据CPU核心数确定线程池并发线程数(转)

    一.抛出问题 关于如何计算并发线程数,一般分两派,来自两本书,且都是好书,到底哪个是对的?问题追踪后,整理如下: 第一派:<Java Concurrency in Practice>即&l ...

  2. 把app(apk和ipa文件)安装包放在服务器上供用户下方法

    怎么把app(apk和ipa文件)安装包放在服务器上供用户下载? IIS服务器网站不能下载.apk文件的原因:IIS的默认MIME类型中没有.apk文件,所以无法下载.解决办法:给.apk格式文件添加 ...

  3. 多线程理论———— threading

    什么是线程 线程也是一种多任务编程方法,可以利用计算机多核资源完成程序的并发执行.线程又被称为轻量级的进程.线程的特征 * 线程是计算机多核分配的最小单位 * 一个进程可以包含多个线程 * 线程也是一 ...

  4. 系统模块 sys 函数的调用

    系统模块 sys 运行时系统相关的信息 sys模块的数据 数据 描述 sys.path 模块搜索路径 path[0] 是当前脚本程序的路径名,否则为 '' sys.modules 已加载模块的字典 s ...

  5. 【2】Kafka概念及原理

    1.Kafka背景 1.1.Kafka概要  Apache Kafka是一个开源的.轻量级的.分布式的.可分区的.可复制备份的.基于zookeeper协调管理的分布式流式消息系统.由Scala写成,支 ...

  6. mysql中varchar可以存多少汉字

    汉字长度与编码有关 MySql 5.0 以上的版本: 1.一个汉字占多少长度与编码有关: UTF-8:一个汉字 = 3个字节,英文是一个字节 GBK: 一个汉字 = 2个字节,英文是一个字节 2.va ...

  7. 2.06_Python网络爬虫_正则表达式

    一:爬虫的四个主要步骤 明确目标 (要知道你准备在哪个范围或者网站去搜索) 爬 (将所有的网站的内容全部爬下来) 取 (过滤和匹配我们需要的数据,去掉没用的数据) 处理数据(按照我们想要的方式存储和使 ...

  8. C++STL库常用函数用法

    开学就要上OOP了.....感觉十分萌萌哒- -! 整理自<ACM程序设计>,本文为转载(原文地址) 迭代器(iterator) 个人理解就是把所有和迭代有关的东西给抽象出来的,不管是数组 ...

  9. CentOS开放端口的方法

    Centos升级到7之后,内置的防火墙已经从iptables变成了firewalld.所以,端口的开启还是要从两种情况来说明的,即iptables和firewalld.更多关于CentOs防火墙的最新 ...

  10. 选择排序Selection_Sort

    基本思想:和冒泡排序.直接插入排序并称为三大简单排序算法.显然,说明它们都很简单