leetcode529
public class Solution {
//DFS
public char[,] UpdateBoard(char[,] board, int[] click)
{
int m = board.GetLength(), n = board.GetLength();
int row = click[], col = click[];
if (board[row, col] == 'M')
{ // Mine
board[row, col] = 'X';
}
else
{ // Empty
// Get number of mines first.
int count = ;
for (int i = -; i < ; i++)
{
for (int j = -; j < ; j++)
{
if (i == && j == ) continue;
int r = row + i, c = col + j;
if (r < || r >= m || c < || c < || c >= n) continue;
if (board[r, c] == 'M' || board[r, c] == 'X') count++;
}
}
if (count > )
{ // If it is not a 'B', stop further DFS.
board[row, col] = (char)(count + '');
}
else
{ // Continue DFS to adjacent cells.
board[row, col] = 'B';
for (int i = -; i < ; i++)
{
for (int j = -; j < ; j++)
{
if (i == && j == ) continue;
int r = row + i, c = col + j;
if (r < || r >= m || c < || c < || c >= n) continue;
if (board[r, c] == 'E') UpdateBoard(board, new int[] { r, c });
}
}
}
}
return board;
}
////BFS
//public char[,] UpdateBoard(char[,] board, int[] click)
//{
// int m = board.GetLength(0), n = board.GetLength(1);
// Queue<int[]> queue = new Queue<int[]>();
// queue.Enqueue(click);
// while (queue.Count > 0)
// {
// int[] cell = queue.Dequeue();
// int row = cell[0], col = cell[1];
// if (board[row, col] == 'M')
// { // Mine
// board[row, col] = 'X';
// }
// else
// { // Empty
// // Get number of mines first.
// int count = 0;
// for (int i = -1; i < 2; i++)
// {
// for (int j = -1; j < 2; j++)
// {
// if (i == 0 && j == 0) continue;
// int r = row + i, c = col + j;
// if (r < 0 || r >= m || c < 0 || c < 0 || c >= n) continue;
// if (board[r, c] == 'M' || board[r, c] == 'X') count++;
// }
// }
// if (count > 0)
// { // If it is not a 'B', stop further DFS.
// board[row, col] = (char)(count + '0');
// }
// else
// { // Continue BFS to adjacent cells.
// board[row, col] = 'B';
// for (int i = -1; i < 2; i++)
// {
// for (int j = -1; j < 2; j++)
// {
// if (i == 0 && j == 0) continue;
// int r = row + i, c = col + j;
// if (r < 0 || r >= m || c < 0 || c < 0 || c >= n) continue;
// if (board[r, c] == 'E')
// {
// queue.Enqueue(new int[] { r, c });
// board[r, c] = 'B'; // Avoid to be added again.
// }
// }
// }
// }
// }
// }
// return board;
//}
}
https://leetcode.com/problems/minesweeper/#/description
提供一种会超时的解决方案:
public char[,] UpdateBoard(char[,] board, int[] click)
{
var row = board.GetLength();//行数
var col = board.GetLength();//列数 var visited = new bool[row, col];//默认为全为false var i = click[];
var j = click[];
if (board[i, j] == 'M')
{
board[i, j] = 'X';
return board;
}
else
{
Queue<int[]> Q = new Queue<int[]>();
var coord = new int[] { i, j };
Q.Enqueue(coord);
var list = new List<int[]>();//存储有效节点
while (Q.Any())
{
var position = Q.Dequeue();
//获取新的坐标
var x = position[];
var y = position[];
visited[x, y] = true;//当前点被访问 //根据毗邻元素更新当前地板的标记
var minecount = ;//八个方向毗邻地板的地雷个数
list.Clear();
//左上
var left_top = new int[] { x - , y - };
if (left_top[] >= && left_top[] >= && !visited[left_top[], left_top[]])
{
if (board[left_top[], left_top[]] == 'M')
{
minecount++;//探测得一枚地雷
}
else
{
list.Add(left_top);//暂存有效节点
}
} //正上
var top = new int[] { x - , y };
if (top[] >= && !visited[top[], top[]])
{
if (board[x - , y] == 'M')
{
minecount++;
}
else
{
list.Add(top);
}
} //右上
var right_top = new int[] { x - , y + };
if (right_top[] >= && right_top[] <= col - && !visited[right_top[], right_top[]])
{
if (board[right_top[], right_top[]] == 'M')
{
minecount++;
}
else
{
list.Add(right_top);
}
} //正右
var right = new int[] { x, y + };
if (right[] <= col - && !visited[right[], right[]])
{
if (board[right[], right[]] == 'M')
{
minecount++;
}
else
{
list.Add(right);
}
} //右下
var right_bottom = new int[] { x + , y + };
if (right_bottom[] <= row - && right_bottom[] <= col - && !visited[right_bottom[], right_bottom[]])
{
if (board[right_bottom[], right_bottom[]] == 'M')
{
minecount++;
}
else
{
list.Add(right_bottom);
}
} //正下
var bottom = new int[] { x + , y };
if (bottom[] <= row - && !visited[bottom[], bottom[]])
{
if (board[bottom[], bottom[]] == 'M')
{
minecount++;
}
else
{
list.Add(bottom);
}
} //左下
var left_bottom = new int[] { x + , y - };
if (left_bottom[] <= row - && left_bottom[] >= && !visited[left_bottom[], left_bottom[]])
{
if (board[left_bottom[], left_bottom[]] == 'M')
{
minecount++;
}
else
{
list.Add(left_bottom);
}
} //正左
var left = new int[] { x, y - };
if (left[] >= && !visited[left[], left[]])
{
if (board[left[], left[]] == 'M')
{
minecount++;
}
else
{
list.Add(left);
}
} //毗邻的八个位置都没有地雷
if (minecount == )
{
//当前节点标记为B
board[x, y] = 'B';
foreach (var l in list)
{
Q.Enqueue(l);
}
}
else
{
char ct = (char)(minecount + '');//int转ascii码
board[x, y] = ct;
}
}
}
return board;
}
}
按照BFS的思路来写,但是判断比较麻烦。
leetcode529的更多相关文章
- [Swift]LeetCode529. 扫雷游戏 | Minesweeper
Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix representin ...
- LeetCode529. 扫雷游戏 Python3 DFS+BFS+注释
https://leetcode-cn.com/problems/minesweeper/solution/python3-dfsbfszhu-shi-by-xxd630/ 规则: 'M' 代表一个未 ...
随机推荐
- How to Change the Size of a Box-Plot Label in MATLAB
Type "load carsmall" to load a sample data set included with MATLAB. Type "boxplot(Ho ...
- 用IdHTTPServer搞个简单的WEB服务器下载文件
放在公司共享盘中的文件,不时就被其他人剪切走了,本想用Apache搭个服务端,提供文件下载的功能,写php脚本时碰到点问题,没折腾出来,一狠心,用Indy的IdHttpServer写.不过中间也碰到了 ...
- jquery动态增加或删除tr和td【实际项目】
难点: (1)动态增加.删除tr和td (2)每天tr和td都有下标,且下标要动态变化, (3)tr和td为什么下标不能随便写,原因是此处需要把所有tr中的数据以list的形式发送到后台对象中,所有每 ...
- Codeforces 808G Anthem of Berland【KMP】【DP】
LINK 简要题意 给你一个串s,上面有字母和一些通配符,问你将通配符换成字母之后最多可以出现多少次串t 首先有一个很傻子的做法就是\(dp_{i,j}\)表示s到第i个位置匹配t串前j个字符的完整t ...
- Oracle数据库安装完成后相关问题的解决
笔者一直以来都是使用公司服务器上的oracle数据库,突然一天公司服务器宕机了,项目无法访问数据库跟着瘫痪了,所以准备在自己的机器上安装一个oracle数据库. 从官网下载安装了oracle 11g后 ...
- ( 转)mappingResource属性和mappingDirectoryLocations属性的使用
在Spring的applicationContext.xml中配置映射文件,通常是在这个Bean实例中进行的,若配置的映射文件较少时,可以用sessionFactory的所属类LocalSession ...
- Arduino+A4988驱动两相四线步进电机
先吐槽一下,在某宝买东西这么多年碰到的不靠谱的卖家也没这几天多.丝杆发短,42电机只有32大,碳杆上的鱼眼粘的没法再歪了还死紧……所以组装还得几天.于是先玩了一下DC-DC降压模块和A4988,规划了 ...
- android 文件上传,中文utf-8编码
要上传文件到后台的php服务器,服务器能收到中文,手机发送过去,却只能收到一堆转了UTF-8的编码(就是要decode后才是中文的编码).android这边上传文件通常是用stream方式上传的,用M ...
- 关闭easyui Tabs,有意思的JS异步处理
因业务需要,需要将关闭windows窗口内的所有Tabs关闭掉,因此写了个方法,执行结果把我惊了一下. function closeAllTabs() { var tabsCount = $(&quo ...
- canvas之画圆
<canvas id="canvas" width="500" height="500" style="background ...