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的更多相关文章

  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. 529. Minesweeper扫雷游戏

    [抄题]: Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix repre ...

  3. LeetCode 529. Minesweeper

    原题链接在这里:https://leetcode.com/problems/minesweeper/description/ 题目: Let's play the minesweeper game ( ...

  4. leetcode笔记(七)529. Minesweeper

    题目描述 Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix repres ...

  5. 529 Minesweeper 扫雷游戏

    详见:https://leetcode.com/problems/minesweeper/description/ C++: class Solution { public: vector<ve ...

  6. [LeetCode] 529. Minesweeper 扫雷

    Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix representin ...

  7. LC 529. Minesweeper

    Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix representin ...

  8. 【LeetCode】529. Minesweeper 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetco ...

  9. 529. Minesweeper

    ▶ 扫雷的扩展判定.已知棋盘上所有点的情况(雷区 'M',已翻开空白区 'B',未翻开空白区 'E',数字区 '1' ~ '8'),现在给定一个点击位置(一定在空白区域),若命中雷区则将被命中的 M ...

随机推荐

  1. 利用webSocket实现浏览器中多个标签页之间的通信

    webSoket用来实现双向通信,客户端和服务端实时通信. webSoket优点和缺点? 优点:对于前端来说,使用简单,功能灵活,如果部署了webSocket服务器,可以实现实时通信. 缺点:需要服务 ...

  2. Python 操作sqlite数据库及保存查询numpy类型数据(二)

    # -*- coding: utf-8 -*- ''' Created on 2019年3月6日 @author: Administrator ''' import sqlite3 import nu ...

  3. OC+swift混编

    作者:fengsh998 原文地址:http://blog.csdn.net/fengsh998/article/details/34440159 转载请注明出处 如果觉得文章对你有所帮助,请通过留言 ...

  4. 继续死磕python

    一.数据运算 算术运算 比较运算 赋值运算 逻辑运算 成员运算 身份运算 位运算 其中左右移运算是逻辑左右移即缺失位补0,而算数右移缺失补符号位(注意逻辑运算都是补码运算即都取补码再运算,然后结果也是 ...

  5. Myabtis中批量更新update多字段

    在mybatis中批量更新多个字段 推荐使用如下操作: 方式1:在Dao层接口中: void updateBatch(@Param("list")List<Student&g ...

  6. 内置json&pickle&shelve&xml

    序列化:把对象(变量)从内存中变成可存储可传输的过程称之为序列化,Python中叫做pickling,其他语言中也被称之为serialization,marshalling,flattening等等 ...

  7. 034-openstack中虚拟机启动后主机名设置问题

    openstack中虚拟机启动后主机名设置问题,在centos7中设置hostname后怎么都是原来的hostname,根本无效. 方法一: 在centos7中除了修改hosts文件和network文 ...

  8. Insomni'hack teaser 2019 - Pwn - 1118daysober

    参考链接 https://ctftime.org/task/7459 Linux内核访问用户空间文件:get_fs()/set_fs()的使用 漏洞的patch信息 https://maltekrau ...

  9. c库函数 rewind fseek

    rewind(3) 将文件内部的位置指针重新指向一个流(数据流/文件)的开头 不是文件指针而是文件内部的位置指针 rewind函数作用等同于 (void)fseek(stream, 0L, SEEK_ ...

  10. 自定义任务状态来操作FreeRTOS任务的挂起,恢复,删除

    osThreadState osState2;//自定义一个线程的状态 osThreadState 系统枚举定义如下: typedef enum { osThreadRunning = 0x0, /* ...