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的更多相关文章

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

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

  2. LeetCode529. 扫雷游戏 Python3 DFS+BFS+注释

    https://leetcode-cn.com/problems/minesweeper/solution/python3-dfsbfszhu-shi-by-xxd630/ 规则: 'M' 代表一个未 ...

随机推荐

  1. IAuthenticationManager.SignOut 退不了

      AuthenticationManager.SignOut(); 这个退不了,然后就加上   AuthenticationManager.SignOut( DefaultAuthenticatio ...

  2. 原生js实现div拖拽+按下鼠标计时

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style> ...

  3. spring beans 源码解读

    从把spring下下来,导入到eclipse,花了几个小时的时间. 本来壮志雄心的说要,满满深入学习研读spring源码,现在看来还是不太现实,太难懂了,各种依赖,说明都是英文,整个串起来理解,深入研 ...

  4. 静态分析工具PMD使用说明

    质量是衡量一个软件是否成功的关键要素.而对于商业软件系统,尤其是企业应用软件系统来说,除了软件运行质量.文档质量以外,代码的质量也是非常重要的.软件开发进行到编码阶段的时候,最大的风险就在于如何保证代 ...

  5. Java Garbage Collection

    在C/C++中,需要自己负责object的creation 和 destruction. 如果忘记了destruction, 就容易出现OutOfMemoryErrors. Java中会有GC直接处理 ...

  6. matplotlib ----- 初步

    直接看几段代码即可: # 加载模块的方式 import matplotlib.pyplot as plt import numpy as np # 最简单的单线图 x = np.linspace(0, ...

  7. [LeetCode系列]最大连续子列递归求解分析

    本文部分参考Discuss: LeetCode. 步骤1. 选择数组的中间元素. 最大子序列有两种可能: 包含此元素/不包含. 步骤2. 步骤2.1 如果最大子序列不包含中间元素, 就对左右子序列进行 ...

  8. bzoj 1185 [HNOI2007]最小矩形覆盖——旋转卡壳

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1185 矩形一定贴着凸包的一条边.不过只是感觉这样. 枚举一条边,对面的点就是正常的旋转卡壳. ...

  9. hadoop深入学习之SequenceFile

    的1个byte 3.Key和Value的类名 4.压缩相关的信息 5.其他用户定义的元数据 6.同步标记,sync marker Metadata 在文件创建时就写好了,所以也是不能更改的.条记录存储 ...

  10. jdk1.8新特性应用之Collection

    之前说了jdk1.8几个新特性,现在看下实战怎么玩,直接看代码: public List<MSG_ConMediaInfo> getConMediaInfoList(String live ...