[抄题]:

Let's play the minesweeper game (Wikipediaonline 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:

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

没啥思路啊,不知道怎么统计雷的数量:单独新开一个函数,for i for j两层循环就行了。

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. dsf开始的条件是一个符合条件的点,直接带入公式就行了,主函数里不再需要做for循环
  2. dfs变化的条件是board[i][j] == 'M'首次出现的条件,后续只需要count++就行了
  3. dfs退出的条件是x太大太小、y太大太小、矩阵中某点不符合规律
  4. dfs的signature是x和x的范围m,y和y的范围n,矩阵名字。

[二刷]:

  1. board[i][j] != 'E'表示已经走过了,dfs不用再走

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

class Solution {
int[][] dirs = {{0,1},{0,-1},{1,0},{1,1},{1,-1},{-1,0},{-1,1},{-1,-1}}; public char[][] updateBoard(char[][] board, int[] click) {
//initialization
int m = board.length; int n = board[0].length;
int x = click[0]; int y = click[1]; //corner case
if (board == null || click == null || m == 0 || n == 0 || click.length != 2) return result; //if detected, turn to x
if (board[x][y] == 'M')
board[x][y] = 'X';
//if not, go dfs
else {
dfs(x, y, m, n, board, dirs);
} //return
return board;
} public void dfs(int i, int j, int m, int n, char[][] board, int[][] dirs) {
//exit
if (i < 0 || i >= m || j < 0 || j >= n || board[i][j] != 'E') return ; //get the count, add num or go further dfs
int count = adjMines(i, j, m, n, board);
if (count > 0) {
board[i][j] = (char)(count + '0');
//change to char in 2 steps
}else {
board[i][j] = 'B';
for (int[] dir : dirs) {
dfs(i + dir[0], j + dir[1], m, n, board, dirs);
}
}
} public int adjMines(int x, int y, int m, int n, char[][] board) {
//count = 0;
int count = 0;
//go in 4 dirs, add count if qualified
for (int i = x - 1; i <= x + 1; i++) {
for (int j = y - 1; j <= y + 1; j++) {
if (0 <= i && i < m && 0 <= j && j < n
&& board[i][j] == 'M')
count++;
}
}
return count;
}
}

[复杂度]:Time complexity: O(mn) Space complexity: O(1)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

529. Minesweeper扫雷游戏的更多相关文章

  1. 529 Minesweeper 扫雷游戏

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

  2. [LeetCode] Minesweeper 扫雷游戏

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

  3. [LeetCode] 529. Minesweeper 扫雷

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

  4. Leetcode之广度优先搜索(BFS)专题-529. 扫雷游戏(Minesweeper)

    Leetcode之广度优先搜索(BFS)专题-529. 扫雷游戏(Minesweeper) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tre ...

  5. [Swift]LeetCode529. 扫雷游戏 | Minesweeper

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

  6. Java实现 LeetCode 529 扫雷游戏(DFS)

    529. 扫雷游戏 让我们一起来玩扫雷游戏! 给定一个代表游戏板的二维字符矩阵. 'M' 代表一个未挖出的地雷,'E' 代表一个未挖出的空方块,'B' 代表没有相邻(上,下,左,右,和所有4个对角线) ...

  7. Leetcode 529.扫雷游戏

    扫雷游戏 让我们一起来玩扫雷游戏! 给定一个代表游戏板的二维字符矩阵. 'M' 代表一个未挖出的地雷,'E' 代表一个未挖出的空方块,'B' 代表没有相邻(上,下,左,右,和所有4个对角线)地雷的已挖 ...

  8. LN : leetcode 529 Minesweeper

    lc 529 Minesweeper 529 Minesweeper Let's play the minesweeper game! You are given a 2D char matrix r ...

  9. Java练习(模拟扫雷游戏)

    要为扫雷游戏布置地雷,扫雷游戏的扫雷面板可以用二维int数组表示.如某位置为地雷,则该位置用数字-1表示, 如该位置不是地雷,则暂时用数字0表示. 编写程序完成在该二维数组中随机布雷的操作,程序读入3 ...

随机推荐

  1. 使用parted对大于2T的磁盘进行分区

    使用parted对磁盘进行分区 版本信息 版本 修改日期 修改人 修改内容 备注 V0.1 2018/09/06   初始化版本 讨论稿                                 ...

  2. Python3之max key参数学习记录

    今天用Python写脚本,想要实现这样的功能:对于给定的字典,返回其中Value最大值对应的Key. 搜索后找到了解决方法,同时也学到了max key参数的作用. 例1, testlist = [9. ...

  3. 如何用原生js开发一个Chrome扩展程序

    原文地址:How to Build a Simple Chrome Extension in Vanilla JavaScript 开发一个Chrome扩展程序非常简单,只需要使用原生的js就可以完成 ...

  4. 第一章 C#入门 (Windows窗体应用程序)(一)

    我的第一个窗体应用程序(一) [案例说明]  在文本框中显示一行文字“Hello C#!”,单击[显示]按钮后在文本框中显示文字:单击[清除]按钮后清除文本框中的内容. [案例实现步骤] 1.新建项目 ...

  5. VSCode TypeScript开发

    下载VSCode VSCode是我使用过最棒的编辑器没有之一. 创建项目 创建目录 首先我们创建一个项目文件夹,比如叫ts_vscode,然后创建一些主要目录,在VSCode中打开项目 目录结构: t ...

  6. ionic3安卓平台引用高德地图

    1.前置条件 第一步,注册高德开发者:第二步,去控制台创建应用:第三步,获取Key. 2.打开src目录下的index.html, 在head标签中添加以下代码,引入js: <script ty ...

  7. 洛谷P1636学画画

    传送 这个题我们需要一个大胆的想法(虽然AC后看了题解知道这是个定理) (求证明qwq) 如果一个图有2或0个奇点,它就一定可以一笔画出,如果不是2或0个奇点,那答案就是奇点数/2 (私认为因为两个奇 ...

  8. JDBC 中preparedStatement和Statement区别

    一.概念 PreparedStatement是用来执行SQL查询语句的API之一,Java提供了 Statement.PreparedStatement 和 CallableStatement三种方式 ...

  9. Centos7在单用户模式下重置root密码

    1.启动Centos7 ,按空格让其停留在如下界面: 鼠标上下可以选择启动内核,默认选择第一个内核开机 2.按e键进入编辑模式 e 按下e键后我们可能无法看到我们需要编辑的区域,这是因为在较新版本的C ...

  10. linux6下源码安装mysql5.6

    概述:CentOS 6.4下通过yum安装的MySQL是5.1版的,比较老,所以就想通过源代码安装高版本的5.6.14.正文:一:卸载旧版本使用下面的命令检查是否安装有MySQL Serverrpm ...