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 ...
随机推荐
- localStorage的使用和vuex的拆分
问题1:在隐身模式.或者用户未启用的情况下,使用localStorage可能会导致浏览器直接报错,怎么办? 方法:使用try-catch包裹 代码示例: store.jsimport Vue from ...
- Ubuntu 增加新用户并赋予root权限及免密的方法
添加用户 添加一个名为hylink的用户 adduser hylink 修改密码 passwd hylink Changing password for user hylink. New UNIX p ...
- 解决solr无法加core
提示缺少配置文件:Error CREATEing SolrCore 'new_core': Unable to create core [new_core] Caused by: Can't find ...
- nmbd - 向客户端提供构造在IP之上的NetBIOS名字服务的NetBIOS名字服务器
总览 SYNOPSIS nmbd [-D] [-F] [-S] [-a] [-i] [-o] [-h] [-V][-d <debug level>] [-H <lmhosts fil ...
- java web请求过程
小技巧: 1.浏览器缓存 Ctrl+F5组合键刷新页面,浏览器会直接向目标URL发送请求,而不会使用浏览器缓存,并会在HTTP请求header中增加下面的请求头来告诉服务器不使用服务器缓存 发现在re ...
- MTK6261之检测是否插了T卡
T卡的更目录可用SRV_FMGR_CARD_DRV 其宏定义如下: #define SRV_FMGR_CARD_DRV FS_GetDrive(FS_DRIVE_V_REMOVABLE, 1, FS_ ...
- L3-006. 迎风一刀斩
迎着一面矩形的大旗一刀斩下,如果你的刀够快的话,这笔直一刀可以切出两块多边形的残片.反过来说,如果有人拿着两块残片来吹牛,说这是自己迎风一刀斩落的,你能检查一下这是不是真的吗? 注意摆在你面前的两个多 ...
- python数据探索与数据与清洗概述
数据探索的核心: 1.数据质量分析(跟数据清洗密切联系,缺失值.异常值等) 2.数据特征分析(分布.对比.周期性.相关性.常见统计量等) 数据清洗的步骤: 1.缺失值处理(通过describe与len ...
- 借用的对vue-cli配置对解析
- springboot + 注解 + 拦截器 + JWT 实现角色权限控制
1.关于JWT,参考: (1)10分钟了解JSON Web令牌(JWT) (2)认识JWT (3)基于jwt的token验证 2.JWT的JAVA实现 Java中对JWT的支持可以考虑使用JJWT开源 ...