LeetCode 529. Minesweeper 解题报告(python)

news/2024/11/7 5:23:31/

529. Minesweeper

  1. Fizz Buzz Multithreaded python solution

题目描述

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:

If a mine (‘M’) is revealed, then the game is over - change it to ‘X’.
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.
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.
Return the board when no more squares will be revealed.

在这里插入图片描述

解析

就是编程实现扫雷游戏喽

class Solution(object):def updateBoard(self, board, click):""":type board: List[List[str]]:type click: List[int]:rtype: List[List[str]]"""if not board or not board[0]:return []i, j = click[0], click[1]if board[i][j] == "M":board[i][j] = "X"return boardm, n = len(board), len(board[0])directions = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]self.dfs(i, j, m, n, board, directions)return boarddef dfs(self, i, j, m, n, board, directions):if board[i][j] != "E":returnmine_count = 0for d in directions:di, dj = i+d[0], j+d[1]if 0<=di<m and 0<=dj<n and board[di][dj] == "M":mine_count += 1if mine_count == 0:board[i][j] = "B"else:board[i][j] = str(mine_count)returnfor d in directions:di, dj = i+d[0], j+d[1]if 0<=di<m and 0<=dj<n:self.dfs(di, dj, m, n, board, directions)

Reference

https://leetcode.com/problems/minesweeper/discuss/137802/Python-DFS-solution


http://www.ppmy.cn/news/884480.html

相关文章

[LeetCode]529. Minesweeper

https://leetcode.com/problems/minesweeper/?tabDescription 计算扫雷游戏点击当前位置后显示结果 BFS&#xff0c;不能一直深搜&#xff0c;因为当前位置如果要显示数字&#xff08;即周围有几个雷&#xff09;&#xff0c;那么不能对它的四周进行深搜。 因此先count当前…

调用接口返回 #x6210;#x529F;

最近项目中遇到一个问题&#xff0c;具体的场景是使用axis2接口调用别人的webservice接口&#xff0c;入参是xml格式&#xff0c;结果请求失败&#xff0c;返回的状态码肯定是失败的状态码&#xff0c;但是返回的失败原因非中文是一串字符&#xff0c;让人很是一头雾水&#xf…

如何选择深度强化学习算法:MuZero/SAC/PPO/TD3/DDPG/DQN/等算法

赶时间请直接看加粗的四种算法,它们占据不同的生态位,请根据实际任务需要去选择他们,在强化学习的子领域(多智能体、分层强化学习、逆向强化学习也会以它们为基础开发新的算法): 离散动作空间推荐:Dueling DoubleDQN(D3QN)连续动作空间推荐:擅长调参就用 TD3,不擅长…

新字符设备驱动实验

目录&#xff1a; 1. 新字符设备驱动原理1.1. 分配和释放设备号1.2. 添加字符设备 2.自动创建设备节点2.1. mdev机制2.2. 创建和删除类2.3. 创建设备2.4. 新字符设备驱动框架总结 3. 文件结构体和文件私有数据4. 实验程序编写4.1. 驱动程序4.2. 应用程序 5. 运行测试 1. 新字符…

网址集合2022

网址集合 部署Consoul 全局捕获异常 less语法 经典的40道js题 reatc-quilljs中文文档 ProComponents java反编译

幼教网址大全

为了方便幼儿园的老师和园长实用好的网站&#xff0c;做了一个幼教网址大全的页面&#xff1a;www.123.51yey.com在这里几乎能找到所有的好的幼教网站。 比如&#xff0c;如果幼儿园招聘老师可以直接在这里找到专业的招聘老师的网站&#xff0c;在这几个网站发布信息会使幼儿园…

App隐私政策网址(URL)

本软件尊重并保护所有使用服务用户的个人隐私权。为了给您提供更准确、更有个性化的服务&#xff0c;本软件会按照本隐私权政策的规定使用和披露您的个人信息。但本软件将以高度的勤勉、审慎义务对待这些信息。除本隐私权政策另有规定外&#xff0c;在未征得您事先许可的情况下…

免费将网址转换二维码

对于网络营销而言&#xff0c;官网影响力不言而喻的&#xff0c;如何将建设好的网站推广出去呢&#xff1f;或许给网站添加二维码&#xff0c;对网站有一定的宣传推广作用&#xff0c;用户可以通过手机扫描网站二维码就可以直接浏览网站&#xff0c;且关注网站。 通过扫描二维…