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 ...
随机推荐
- Optional接口简记
@Data public class Employee { private String name; } @Data public class Company { private String nam ...
- HTML-图片和多媒体
1.图片和多媒体 (1) 图片:img元素 src 属性:图片路径: alt 属性:图片无法显示时使用的替代文字: title:鼠标悬停时显示的文字 : <img src="图片 ...
- 系统环境: CentOS 64位+千万不要在生产环境中升级glibc!
# cd /lib64# LD_PRELOAD=/lib64/libc-2.15.so ln -sf /lib64/libc-2.15.so libc.so.6 libc-2.15.so 这个文件名根 ...
- PAT Basic 1014 福尔摩斯的约会 (20 分) Advanced 1061 Dating (20 分)
大侦探福尔摩斯接到一张奇怪的字条:我们约会吧! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm.大侦探很快就明白了,字条上奇 ...
- Python chardet字符编码的判断
使用 chardet 可以很方便的实现字符串/文件的编码检测.尤其是中文网页,有的页面使用GBK/GB2312,有的使用UTF8,如果你需要去爬一些页面,知道网页编码很重要的,虽然HTML页面有cha ...
- windows 10 安装openssh 0x800f0954 的一种解决方法
安装与卸载参考:https://docs.microsoft.com/zh-cn/windows-server/administration/openssh/openssh_install_first ...
- Linux C编程学习
C语言简介 简介 C语言具有控制特性较强.高效性.可移植性和强大的功能和灵活性."自由的代价是永远的警惕",C的简洁性与其丰富的运算符相结合,使其可能会编写出较难理解的代码.面向对 ...
- 洛谷p3955 图书管理员(NOIP2017 t2)
蒟蒻的最后一篇pas题解...目前转c++ ing 回顾了一下,发现c++的string真的好繁啊(主要我这个蒟蒻太菜不会用) 还是pas的string操作简洁 做法 我这种蒟蒻不像别的dalao,懒 ...
- Dancing Stars on Me
Dancing Stars on Me Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Ot ...
- Oracle--利用监听器日志监控访问该数据库的客户端IP
服务器10.10.10.168 数据库seineebs 客户端 10.10.10.14 用户guipeng.zhang 查看监听器状态: 在本机利用PL/SQL工具连接该数据库 查看监听器日志:一 ...