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. 图文详解如何利用Git+Github进行团队协作开发

    团队协作开发中,大部分都会用到版本控制软件,比如Git.Svn等.本文将通过一个实例,详细讲解在真实的工作环境中,一个团队应该如何利用Git+Github进行协作开发,即详解Git工作流程.并就其中比 ...

  2. 折腾 Phalcon 的笔记

    不要用 IIS!Apache 万岁! 不要用 Web Platform Installer!自己动手丰衣足食! 注意版本号.TS 与 NonTS.x86 与 x64 的区别! 关于 Apache 的配 ...

  3. Maven系列(一)之初识Maven

    Maven是个啥? Maven主要服务于基于Java平台的项目构建.依赖管理和项目信息管理,并且Maven是跨平台的,这意味着无论是在Windows上,还是在Linux或者Mac上,都可以使用同样的命 ...

  4. hadoop2.x配合ZooKeeper集群环境搭建

    前期准备就不详细说了,课堂上都介绍了1.修改Linux主机名2.修改IP3.修改主机名和IP的映射关系 ######注意######如果你们公司是租用的服务器或是使用的云主机(如华为用主机.阿里云主机 ...

  5. Nchan 实时消息内置变量

      以下参考官方文档:   $nchan_channel_idThe channel id extracted from a publisher or subscriber location requ ...

  6. CentOS下编译安装LNMP环境

    一.卸载系统预安装的LAMP软件 rpm -qa|grep httpd rpm -e httpd httpd-tools rpm -qa|grep mysql rpm -e mysql mysql-l ...

  7. 快速排序算法-python实现

    #-*- coding: UTF-8 -*- import numpy as np def Partition(a, i, j): x = a[i] #将数组的第一个元素作为初始基准位置 p = i ...

  8. python中format函数学习笔记

    简而言之,format函数就是用{}来代替之前的输出字符时使用的% print('my name is %s and I am %d years old' % ('porsche',23)) 下面详细 ...

  9. HotSpot Stop-and-Copy GC

    rednaxelafx的Cheney算法的伪代码.如果不用forwarding的话,维护一个旧地址到新地址的映射也可以. 其中重点部分: void Heap::collect() { // The f ...

  10. Windows 键盘快捷键概述

    在 Windows 中工作时,利用快捷键代替鼠标.可以利用键盘快捷键打开.关闭和导航“开始”菜单.桌面.菜单.对话框以及网页.键盘还可以让您更简单地与计算机交互.  单击一个标题或按 TAB 键可以突 ...