• 题目描述

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. Java中的锁之乐观锁与悲观锁

    1.  分类一:乐观锁与悲观锁 a)悲观锁:认为其他线程会干扰本身线程操作,所以加锁 i.具体表现形式:synchronized关键字和lock实现类 b)乐观锁:认为没有其他线程会影响本身线程操作, ...

  2. intellij idea里神坑的@autowire

    当你写完项目的时侯serviceimpl层下的@autowire->对应的是dao层的注入,其下面会出现一条红线 在Intellij Idea开发工具在@Autowired或者@Resource ...

  3. HDU 5215 Cycle(dfs判环)

    题意 题目链接 \(T\)组数据,给出\(n\)个点\(m\)条边的无向图,问是否存在一个奇环/偶环 Sol 奇环比较好判断吧,直接判是否是二分图就行了.. 偶环看起来很显然就是如果dfs到一个和他颜 ...

  4. 【基础笔记】《html&CSS设计与构造网站》一书导读

    ◉HTML 1.结构网页使用HTML HyperText Markup Language 来描述页面结构超文本标记语言允许对文本建立链接,允许对文本进行标记网页开头都有一个DOCTYPE 文档类型 声 ...

  5. String变量的两种创建方式

    在java中,有两种创建String类型变量的方式: String str01="abc";//第一种方式 String str02=new String("abc&qu ...

  6. matlab练习程序(随机直线采样)

    我只是感觉好玩,写了这样一段程序. 原理就是先随机生成两个点,然后根据这两个点画直线,最后在直线上的像素保留,没在直线上的像素丢弃就行了. 最后生成了一幅含有很多空洞的图像. 当然,对含有空洞的图像是 ...

  7. Azure 10月新公布

    Azure 10月新发布:F 系列计算优化实例,认知服务,媒体服务流式处理单元更名,Azure 镜像市场,FreeBSD 适用于Azure 虚拟机的全新 F 系列计算优化实例 Azure 虚拟机的全新 ...

  8. 本地数据库(sql server)插入一条新数据时,同步到服务器数据库

    之前有个同学问我,本地数据库插入新数据时怎么同步到服务器上,当时我先想到是程序逻辑控制,作相应的处理. 但有时候我们程序不太好处理,那能不能从数据库入手呢,数据库不是有触发器(Trigger)吗,应该 ...

  9. WAKE-WIN10-SOFT-CMAKE

    1,CMAKE 官网:https://cmake.org/ 下载:https://cmake.org/download/ BING:https://www.bing.com/search?q=cmak ...

  10. 团队第四次SCrum

    scrum 第四次冲刺 一.项目目的        为生活在长大的学生提供方快捷的生活服务,通过帖子发现自己志同道合的朋友,记录自己在长大点滴.本项目的意义在于锻炼团队的scrum能力,加强团队合作能 ...