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. Access restriction: The type 'JPEGImageWriter' is not API

    报错: Access restriction: The type 'JPEGImageWriter' is not API due to restriction on required library ...

  2. python操作mysql方法和常见问题

    http://www.cnblogs.com/ma6174/archive/2013/02/21/2920126.html 安装mysql模块 sudo easy_install mysql-pyth ...

  3. Django学习系列之django restframework

    曾几何时,Ajax已经统治了Web开发中的客户端,而REST成为web世界中最流行的架构风格(architecture style).所以我们的选择变得很简单:前端ajax访问后端的RESTful A ...

  4. 面试题之strcpy/strlen/strcat/strcmp的实现

    阿里的电面要我用C/C++实现一个字符串拷贝的函数,虽然以前写过 strcpy 的函数实现,但时间过去很久了,再加上有点紧张,突然就措手不及了.最后写是写出来了,但没考虑异常的情况,面试官好像很不满意 ...

  5. C语言++a与a++的实现机制与操作符结合优先级

    看到一道"经典Linux C"面试题,关于左值和右值的. 华为笔试题 1.写出推断ABCD四个表达式的是否正确, 若正确, 写出经过表达式中 a的值(3分) int a = 4; ...

  6. selenium第三课(selenium八种定位页面元素方法)

    selenium webdriver进行元素定位时,通过seleniumAPI官方介绍,获取页面元素的方式一共有以下八种方式,现按照常用→不常用的顺序分别介绍一下. 官方api地址:https://s ...

  7. MyEclipse+Tomcat+MAVEN+SVN项目完整环境搭建

    这次换了台电脑,所以须要又一次配置一次项目开发环境,过程中的种种,记录下来,便于以后再次安装.同一时候给大家一个參考. 1.JDK的安装 首先下载JDK,这个从sun公司官网能够下载.依据自己的系统选 ...

  8. Linux环境部署(一)

    最近被老大安排了个任务,解决Linux的安装部署问题,特做如下笔记,以便下次安装配置: --------------------Linux上部署项目------------------- 1.解压缩相 ...

  9. .NET下为百度文本编辑器UEditor增加图片删除功能

    [摘要:比来写了个项目,用到了UEditor,但是UE并出有文件删除功效 然后网上找若何增加 找半天只能找到一个1.2.X的 以是便摹仿PHP的 改成了.NET的 PHP本文 第一步 (增加背景删除地 ...

  10. H264--1--编码原理以及I帧B帧P帧[4]

    ---------------------- 前言 ----------------------- H264是新一代的编码标准,以高压缩高质量和支持多种网络的流媒体传输著称,在编码方面,我理解的他的理 ...