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 ...
随机推荐
- 001-supervisor
supervisor 使用教程(转) 原文地址:https://word.gw1770df.cc/2016-08-04/linux/supervisor-%E4%BD%BF%E7%94%A8%E6%9 ...
- linux extglob模式 和rm反选,除了某个文件外的其他文件全部删除的命令
1.extglob模式开启之后Shell可以另外识别出5个模式匹配操作符,能使文件匹配更加方便. 不然不识别 #开启命令: shopt -s extglob #关闭命令: shopt -u extgl ...
- Codeforces 912 质因数折半 方格数学期望
A B #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #d ...
- Codeforces 938 正方形方格最多0/1 足球赛dijkstra建图
A #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #def ...
- pipeline语法学习日记
1.pipeline 整合job的通用代码,比较基本 2.pipeline参数化构建
- Spring Boot 整合Spring Data JPA
Spring Boot整合Spring Data JPA 1)加入依赖 <dependency> <groupId>org.springframework.boot</g ...
- java:投个票程序
投票城市用到了:system.in, 正则pattern,matcher,排序接口comparable 复写compareTo排序方法 一个班级在选班长,按序号进行投票,并将票数最高的放在第一位显示 ...
- Java面试之集合框架篇(3)
21.ArrayList和Vector的区别 这两个类都实现了List接口(List接口继承了Collection接口),他们都是有序集合,即存储在这两个集合中的元素的位置都是有顺序的,相当于一种动态 ...
- 1. Spring Security 框架简介
官网:https://projects.spring.io/spring-security/Spring Security 是强大的,且容易定制的实现认证,与授权的基于 Spring 开发的框架.Sp ...
- 【bzoj2724】[Violet 6]蒲公英
*题目描述: *输入: 修正一下 l = (l_0 + x - 1) mod n + 1, r = (r_0 + x - 1) mod n + 1 *输出: *样例输入: 6 3 1 2 3 2 1 ...