[LeetCode]529. Minesweeper
来源:互联网 发布:关于网络推广员的文章 编辑:程序博客网 时间:2024/04/30 01:32
https://leetcode.com/problems/minesweeper/?tab=Description
计算扫雷游戏点击当前位置后显示结果
BFS,不能一直深搜,因为当前位置如果要显示数字(即周围有几个雷),那么不能对它的四周进行深搜。
因此先count当前四周雷数,如果没有雷才能继续递归搜索
public class Solution { int[][] dirs = {{1, 0}, {1, 1}, {1, -1}, {-1, 0}, {-1, 1}, {-1, -1}, {0, 1}, {0, -1}}; public char[][] updateBoard(char[][] board, int[] click) { if (board == null || board.length == 0) { return board; } int i = click[0]; int j = click[1]; if (board[i][j] == 'M') { board[i][j] = 'X'; return board; } update(board, i, j); return board; } private void update(char[][] board, int i, int j) { if (board[i][j] != 'E') { return; } int cnt = 0; for (int[] dir : dirs) { int row = dir[0] + i; int col = dir[1] + j; if (row >= 0 && row < board.length && col >= 0 && col < board[0].length && board[row][col] == 'M') { cnt++; } } board[i][j] = '*'; if (cnt == 0) { board[i][j] = 'B'; for (int[] dir : dirs) { int row = dir[0] + i; int col = dir[1] + j; if (row >= 0 && row < board.length && col >= 0 && col < board[0].length) { update(board, row, col); } } } else { board[i][j] = (char) (cnt + '0'); } }}
https://leetcode.com/problems/minesweeper/?tab=Description
0 0
- Leetcode 529. Minesweeper
- LeetCode: 529. Minesweeper
- [LeetCode]529. Minesweeper
- [leetcode] 529. Minesweeper
- [leetcode] 529. Minesweeper
- 【Leetcode】529. Minesweeper
- LeetCode 529. Minesweeper
- 【LeetCode】529. Minesweeper
- leetcode 529. Minesweeper
- [leetcode]529. Minesweeper
- [LeetCode] 529. Minesweeper
- LeetCode:529. Minesweeper
- LeetCode 529. Minesweeper
- LeetCode 529. Minesweeper 解题报告
- #leetcode编程日记#529. Minesweeper
- [Leetcode] 529. Minesweeper 解题报告
- 529. Minesweeper
- 529. Minesweeper
- NAT DHCP WWW rc.local
- Linux查看网络链接性能
- React Native创建原生模块
- CentOS 6.2 下安装QQ2012完全可用
- Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/base/Function问题解决
- [LeetCode]529. Minesweeper
- <!–[if lte IE ]>……<![endif]–>详解
- 为php安装redis扩展模块并测试
- 简单字符驱动程序mycdev_globalmem.c
- 简单字符驱动程序mycdev.c
- RHEL5.4 两个网段通过双网卡,DHCP配置成功
- C# object 类型
- 通过编辑文件的方式对DNS服务器进行配置
- dhcpd.conf.txt