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的思路来写,但是判断比较麻烦。

最新文章

  1. 敏捷开发XP
  2. npm库下载缓慢解决方案
  3. iOS 拨打电话三种方法
  4. android中的空格及汉字的宽度
  5. fastCGI (二)各方优劣
  6. OpenGl从零开始之坐标变换(上)
  7. iOS开发——混编Swift篇&amp;OC移植为swift
  8. 感动前行——给医学媳妇写的演讲稿(非IT类)
  9. IOS-Archiver文件归档(2)
  10. NSTimer 销毁问题 和 iOS中控制器的释放问题
  11. MVC在VIEW中动态控制htmlAttributes的方法
  12. Numpy 操作
  13. centos7非centos标准服务 /etc/init.d/service_name start || stop 启动异常
  14. [zz]LyX中文问题
  15. QInputDialog Multiple Inputs 输入多个变量的对话框
  16. this 指向 及 调用方式
  17. libgdx学习记录12——圆角矩形CircleRect
  18. caffe安装编译问题-ImportError: No module named google.protobuf.internal
  19. 上Google Adsense个人的一点体验
  20. Web Server IIS7部署网站常遇到的错误及解决办法

热门文章

  1. 为Linux服务器的SSH登录启用Google两步验证
  2. HihoCoder1049 后序遍历 分治水题
  3. 【2018.06.26NOIP模拟】T3节目parade 【支配树】*
  4. vs中无法找到头文件
  5. c++ queue 用法
  6. as3 阻止后续侦听器
  7. 【转】一步一步教你在Ubuntu12.04搭建gstreamer开发环境
  8. 6.Python使用Pandas小案例
  9. javascript 中的 arguments,callee.caller,apply,call 区别
  10. 如何在 Linux 上使用 x2go 设置远程桌面