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' 代表一个未 ...
随机推荐
- matlab 相关系数的计算
1. 首先说说自相关和互相关的概念. 这 个是信号分析里的概念,他们分别表示的是两个时间序列之间和同一个时间序列在任意两个不同时刻的取值之间的相关程度,即互相关函数是描述随机信号 x(t),y ...
- 同一台电脑的多ssh 配置
转载自:https://w3ctrain.com/2016/03/06/mutiple-ssh-key/ ps:此文基于你已经能够正常的生成sshkey 对于sshkey的功能这里就不多做介绍了,本文 ...
- Jmeter-Threads(Users)
setUp Thread Group---测试开始前的准备操作,每次测试前都会执行 A special type of ThreadGroup that can be utilized to perf ...
- BZOJ2763 JLOI2011 飞行路线 【最短路+DP】
BZOJ2763 JLOI2011 飞行路线 Description Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n ...
- Django之Models
1.数据库的配置 1 django默认支持sqlite,mysql, oracle,postgresql数据库. <1> sqlite django默认使用sqlite的数据库,默 ...
- python: find the index of a given value in a list
["foo", "bar", "baz"].index("bar")
- python模块--os模块、sys模块
一.os模块 1 os.getcwd() 获取当前工作的目录,即当前python脚本工作的目录路径 2 3 os.chdir("dirname") 改变当前脚本的工作目录:相当于s ...
- openssl 查看证书细节
打印证书的过期时间 openssl x509 -in signed.crt -noout -dates 打印出证书的内容: openssl x509 -in cert.pem -noout -text ...
- UML基本架构建模--类的术语和概念
Terms and Concepts 术语和概念 A classis a description of a set of objects that share the same attrib ...
- FastAdmin 中 Bootstrap-Table 列宽控制
FastAdmin 中 Bootstrap-Table 列宽控制 使用 css 控制 1 使用 formatter 处理 2 http://issues.wenzhixin.net.cn/bootst ...