lc 529 Minesweeper


529 Minesweeper

Let's play the minesweeper 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']]

Explanation:

Note:

  1. The range of the input matrix's height and width is [1,50].
  2. The click position will only be an unrevealed square ('M' or 'E'),

    which also means the input board contains at least one clickable

    square.
  3. The input board won't be a stage when game is over (some mines have

    been revealed).
  4. 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.

递归 Accepted##

扫雷游戏,其实还挺简单的,注意判断是否在版图之内,利用递归不断推算并且更新。

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';
return board;
}
reveal(board, click[0], click[1]);
return board;
} int out(vector<vector<char>>& board, int x, int y) {
return (x < 0 || y < 0 || x >= board.size() || y >= board[0].size());
} void reveal(vector<vector<char>>& board, int x, int y) {
if (out(board, x, y)) return;
if (board[x][y] == 'E') {
int m = 0;
if (!out(board, x-1, y-1) && board[x-1][y-1] == 'M') m++;
if (!out(board, x-1, y) && board[x-1][y] == 'M') m++;
if (!out(board, x-1, y+1) && board[x-1][y+1] == 'M') m++;
if (!out(board, x, y-1) && board[x][y-1] == 'M') m++;
if (!out(board, x, y+1) && board[x][y+1] == 'M') m++;
if (!out(board, x+1, y-1) && board[x+1][y-1] == 'M') m++;
if (!out(board, x+1, y+1) && board[x+1][y+1] == 'M') m++;
if (!out(board, x+1, y) && board[x+1][y] == 'M') m++;
if (m) {
board[x][y] = '0'+m;
} else {
board[x][y] = 'B';
reveal(board, x-1, y-1);
reveal(board, x-1, y);
reveal(board, x-1, y+1);
reveal(board, x, y-1);
reveal(board, x, y+1);
reveal(board, x+1, y-1);
reveal(board, x+1, y+1);
reveal(board, x+1, y);
}
}
}
};

LN : leetcode 529 Minesweeper的更多相关文章

  1. LeetCode 529. Minesweeper

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

  2. [LeetCode] 529. Minesweeper 扫雷

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

  3. Week 5 - 529.Minesweeper

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

  4. leetcode笔记(七)529. Minesweeper

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

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

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

  6. 529 Minesweeper 扫雷游戏

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

  7. 529. Minesweeper扫雷游戏

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

  8. [LeetCode] 529. Minesweeper_ Medium_ tag: BFS

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

  9. LN : leetcode 399 Evaluate Division

    lc 399 Evaluate Division 399 Evaluate Division Equations are given in the format A / B = k, where A ...

随机推荐

  1. int *ptr=(int *)(&a+1)问题的探讨

    从网络上看到这样一道有意思的题目,是关于数组与指针的问题,描述如下: main() { ]={,,,,}; ); printf(),*(ptr-)); } 输出为:2,5 请解释以上代码的输出结果. ...

  2. JDBC示例(增删查改)

    前提: 1.项目中引入MySQL的JAR包,POM参考如下配置: <!-- mysql-connector-java --> <!-- http://mvnrepository.co ...

  3. OCP知识点讲解 之 队列、资源与锁:RHCA|OCM|CCIE RedHat大中华地区前50位RHCA系统架构师:叶绍琛

      一.队列与共享资源 共享资源可以被多个会话.进程同时访问,因此它的访问需要保护.Oracle中,除了PGA,所有的东西(包括内存.磁盘.CPU.表.索引.事务等等,种类太多,一概用东西两字来代表) ...

  4. 【CV论文阅读】Detecting events and key actors in multi-person videos

    论文主要介绍一种多人协作的视频事件识别的方法,使用attention模型+RNN网络,最近粗浅地学习了RNN网络,它比较适合用于处理序列的存在上下文作用的数据. NCAA Basketball数据集 ...

  5. [tarjan] 1827 Summer Holiday

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1827 Summer Holiday Time Limit: 10000/1000 MS (Java/ ...

  6. SpringMVC之application-context.xml,了解数据库相关配置

    上一篇SpringMVC之web.xml让我们了解到配置一个web项目的时候,怎样做基础的DispatcherServlet相关配置.作为SpringMVC上手的第一步.而application-co ...

  7. CI 知识 :Git介绍及常用操作

    Git介绍 Git(读音为/gɪt/.)是一个开源的分布式版本控制系统,可以有效.高速的处理从很小到非常大的项目版本管理. Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发 ...

  8. 开发人员调试工具Chrome Workspace

    Workspace是个什么样的东西呢?他可以在开发人员工具中调试改动js或者css同一时候自己主动保存文件.可以避免开发人员在工具中调试好,再到编辑器中改动一次代码的反复操作,可以提高一定的效率 配置 ...

  9. vs2012下安装Cocos2d-x模板问题

    今天想開始学Cocos2d-x.于是依据书本的提示到网上去下载了所需的安装包.我下载的cocos2d-x版本号是2.2.3.在下载完毕之后依照书中的步骤对其环境进行配置.在搞到模板安装这一步,发现找不 ...

  10. iOS开发——高级篇——iOS 强制退出程序APP代码

    1.先po代码 UIAlertView* alert = [[UIAlertView alloc] initWithTitle:self.exitapplication message:@" ...