• 题目描述

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.

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:

  1. The range of the input matrix's height and width is [1,50].
  2. The click position will only be an unrevealed square ('M' or 'E'), which also means the input board contains at least one clickable square.
  3. The input board won't be a stage when game is over (some mines have been revealed).
  4. 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.
  • 解题思路:

题目属于背景复杂,但本质不难的类型。因为没玩过扫雷游戏,所以看了半天题目,了解了规则后,可以发现属于传统的广度优先搜索问题。基础实现的时候,会超时间。

最后改了半天边缘问题,仍然是超时。最后,看了英文版leetcode的讨论区,发现就一行神秘的代码,就可以解决重复处理节点的问题~~~

  • 示例代码
class Solution {
public:
vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click) {
int row = click[];
int col = click[]; int maxRow = board.size();
int maxCol = board[].size();
vector<vector<char>> results;
for(int i = ; i < maxRow; i++)
{
vector<char> temp(board[i]);
results.push_back(temp);
}
// M
if(results[row][col] == 'M')
results[row][col] = 'X';
// E
else if(results[row][col] == 'E')
{
queue<pair<int, int>> eNodes;
eNodes.push(make_pair(row, col));
while(!eNodes.empty())
{
pair<int, int> curr = eNodes.front();
eNodes.pop();
row = curr.first;
col = curr.second;
results[row][col] = '';
// check neighbors
for(int i = -; i <= ; i++)
{
int currRow = row + i;
if(currRow < maxRow && currRow >= )
{
for(int j = -; j <= ; j++)
{
if(i == && j == )
continue;
int currCol = col + j;
if(currCol < maxCol && currCol >= )
{
// M add to counter
if(results[currRow][currCol] == 'M')
{
results[row][col]++;
}
}
}
}
}
// around no M
if(results[row][col] == '')
{
results[row][col] = 'B';
// add neighbors to deal
for(int i = -; i <= ; i++)
{
int currRow = row + i;
if(currRow < maxRow && currRow >= )
{
for(int j = -; j <= ; j++)
{
if(i == && j == )
continue;
int currCol = col + j;
if(currCol < maxCol && currCol >= )
{
// E add to deal
if(results[currRow][currCol] == 'E')
{
eNodes.push(make_pair(currRow, currCol));
results[currRow][currCol] = 'B'; // optimize to avoid repeatly added
}
}
}
}
}
}
}// end of dealing
} return results; }
};

leetcode笔记(七)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. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  3. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  4. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  5. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  6. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  7. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  8. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

  9. Leetcode 笔记 36 - Sudoku Solver

    题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...

  10. Leetcode 笔记 35 - Valid Soduko

    题目链接:Valid Sudoku | LeetCode OJ Determine if a Sudoku is valid, according to: Sudoku Puzzles - The R ...

随机推荐

  1. Nodejs 如何制作命令行工具

    # 全局安装,安装报错是需要前面加上sudo $ sudo npm install -g xxxb # 输出帮助 $ xxxb -h Usage: xxxb 这里是我私人玩耍的命令哦![options ...

  2. 针对 IE的 的优化

    针对 IE 的优化 有些时候,你需要对 IE 浏览器的 bug 定义一些特别的规则,这里有太多的 CSS 技巧(hacks),我只使用其中的两种方法,不 管微软在即将发布的 IE7 beta 版里是否 ...

  3. Git回退到指定节点的版本

    1.获取某个历史版本的id(即change-id,每个版本唯一) 方法1:使用git log命令查看所有的历史版本,输入q便可退出. git log 方法2:使用gitk图形化界面查看节点信息.(在安 ...

  4. Xshell连接不上虚拟机的问题和解决办法

    第一次用xshell,一直连不上linux,搞了好久,也查了很多办法,但是最后也终于解决了,在这里我分享一下自己的解决办法,再列举网上的办法,希望可以帮助其他人. 1,你的linux ip地址没有配置 ...

  5. ArcGIS Enterprise 10.5.1 静默安装部署记录(Centos 7.2 minimal)- 4、安装 ArcGIS for Server

    安装ArcGIS for Server 解压server安装包,tar -xzvf ArcGIS_Server_Linux_1051_156429.tar.gz 切换到arcgis账户静默安装serv ...

  6. 初学jboss

    1.简单安装-环境变量配置-创建控制台用户并访问控制台.   下载了windows版的jboss服务器(jboss-as-7.1.1.Final)     依赖JDK1.6以上版本,jdk环境变量等就 ...

  7. Hadoop ->> Hadoop是什么?

    Hadoop是什么? 1)Hadoop是一个分布式计算平台,程序员可以在不需要知道底层结构的情况下实现集群并行运算: 2)Hadoop不只是一个软件或者系统,它代表的是一个生态圈,一个做大数据分析计算 ...

  8. Dynamics CRM 之汇总字段

    用插件汇总数据,速度很慢,导数据的时候更慢!那就用汇总字段- - 新建个汇总字段,字段类型选择汇总.点击编辑进入逻辑编辑 相关实体:对当前实体或者相关联的实体的字段值进行判断筛选. 筛选器:对相关实体 ...

  9. submit text3 常用快捷键

    Ctrl+D : 选择单词,重复可增加选择下一个相同的单词 Ctrl+F : 查找内容 Ctrl+G : 跳转到指定行 Ctrl+H : 替换 Ctrl+J : 合并行(已选择需要合并的多行时) Ct ...

  10. 2017软件测试_HW1_最近遇到的编程问题

     最近遇到的错误:我对着网页源代码编写了一段爬虫语句,运行没有提示有错误,而且 可以抓取到全部的数据,但是不能按照要求将这些数据分到制定的位置. 发现问题原因:我把抓取到的字段对着网页源码看了一下,发 ...