Week 5 - 529.Minesweeper
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:
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']]
Note:
The range of the input matrix's height and width is [1,50].
The click position will only be an unrevealed square ('M' or 'E'), which also means the input board contains at least one clickable square.
The input board won't be a stage when game is over (some mines have been revealed).
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.
这道题总体而言是一个DFS算法,从click on的位置开始向周围八个方向开始搜索。
Solution:
#include<string>
#include<iostream>
#include<vector>
#include<list>
using namespace std;
class Solution {
public:
vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click) {
if (board[click[0]][click[1]] == 'M') {
board[click[0]][click[1]] = 'X';
} else {
reveal(board, click[0], click[1]);
}
return board;
}
bool ifInBoard(vector<vector<char>> board, int x, int y) {
return (x >= 0 && y >= 0 && x < board.size() && y < board[0].size());
}
int searchAndCount(vector<vector<char>> board, int x, int y) {
int count = 0;
if (ifInBoard(board, x - 1, y - 1) && board[x - 1][y - 1] == 'M') count++;
if (ifInBoard(board, x , y - 1) && board[x ][y - 1] == 'M') count++;
if (ifInBoard(board, x + 1, y - 1) && board[x + 1][y - 1] == 'M') count++;
if (ifInBoard(board, x - 1, y ) && board[x - 1][y ] == 'M') count++;
if (ifInBoard(board, x + 1, y ) && board[x + 1][y ] == 'M') count++;
if (ifInBoard(board, x - 1, y + 1) && board[x - 1][y + 1] == 'M') count++;
if (ifInBoard(board, x , y + 1) && board[x ][y + 1] == 'M') count++;
if (ifInBoard(board, x + 1, y + 1) && board[x + 1][y + 1] == 'M') count++;
return count;
}
void reveal(vector<vector<char>>& board, int x, int y) {
if (ifInBoard(board, x, y)) {
if (board[x][y] == 'E') {
int count = searchAndCount(board, x, y);
if (count > 0) {
board[x][y] = count + '0';
} else {
board[x][y] = 'B';
reveal(board, x - 1, y - 1);
reveal(board, x, y - 1);
reveal(board, x + 1, y - 1);
reveal(board, x - 1, y);
reveal(board, x + 1, y);
reveal(board, x - 1, y + 1);
reveal(board, x, y + 1);
reveal(board, x + 1, y + 1);
}
}
}
}
};
//test code
int main() {
Solution a;
vector<char> row = { 'E', 'E', 'E', 'E', 'E' };
vector<vector<char>> board;
board.push_back(row);
row[2] = 'M';
board.push_back(row);
row[2] = 'E';
board.push_back(row);
board.push_back(row);
vector<int> temp = { 3, 0 };
board = a.updateBoard(board, temp);
for (int i = 0; i < board.size(); i++) {
for (int j = 0; j < board[0].size(); j++) {
cout << board[i][j] << ' ';
}
cout << endl;
}
system("pause");
}
Week 5 - 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 ...
- 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 ...
- LC 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 ...
随机推荐
- 虚拟机的网卡基本配置和基本linux命令
1.切换到/etc/sysconfig/network-script目录 cd /etc/sysconfig/network-scripts 2.将ifcfg-eth0备份成ifcfg-eth0. c ...
- Docker数据目录相关操作
数据目录挂载 我们可以在创建容器的时候,将宿主机的目录与容器内的目录进行映射,这样我们就可以通过修改宿主机某个目录的文件从而去影响容器.使用 -v 选项 docker run -id -v /宿主机绝 ...
- C#基础知识之DirectorySearcher 类
活动目录(Active Directory)是从一个数据存储开始的,它采用了类似Exchange Server的数据存储,所以被称为Extensible Storage Service (ESS).其 ...
- css3-background clip 和background origin
1.background-origin background-origin 里面有3个参数 : border-box | padding-box | content-box; border-box,p ...
- BZOJ4625 [BJOI2016]水晶 最小割
题意简述 给你一个三维的坐标系,坐标系上 \((x_i+y_i+z_i)\bmod 3 = 0\) 的点内有能量源.给定 \(n\) 个点含有能量值为 \(c_i\) 的水晶,如果一个水晶位于能量源上 ...
- Vuejs使用scoped(私有) style为v-html中标签添加CSS样式
最近使用Vue框架的时候遇到一个问题,就是后台把数据写好了,而且写好的数据的某些内容是html格式的,使用Vue框架的v-html虽然可以很简单的就把数据转换成html的标签渲染在页面上,但是有些样式 ...
- 【hackerrank】Type of Triangle
题目如下: Write a query identifying the type of each record in the TRIANGLES table using its three side ...
- LeetCode--142--环形链表II(python)
给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 - ...
- linux运维、架构之路-Git+Jenkins实现自动化部署
一.Jenkins介绍 jenkins是一个用JAVA编写的开源的持续集成工具,运行在servlet容器中,支持软件配置管理(SCM)工具,可以执行基于APACHE ANT和APAC ...
- SQL server 统计分组经计
SUM(A.AREA) OVER ( PARTITION BY A.ItemNo, A.PARTS ,A.WIDTH,A.HEIGHT) allotQty, SUM(A.SL) OVER ( PART ...