529. Minesweeper
▶ 扫雷的扩展判定。已知棋盘上所有点的情况(雷区 'M',已翻开空白区 'B',未翻开空白区 'E',数字区 '1' ~ '8'),现在给定一个点击位置(一定在空白区域),若命中雷区则将被命中的 M 改为 X,若命中空白区则将点击位置扩展为带有数字边界的安全区。
● 自己的解法,28 ms,深度优先遍历。改善了边界判定的方法,以后写类似的矩阵函数的时候可以借鉴。实际上可以在 extend 开头判定 click 是否在棋盘范围内,以后就可以强行 8 个方向搜索(见后面大佬的代码)
class Solution
{
public:
vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click)
{
if (board[click[]][click[]] == 'M')
{
board[click[]][click[]] = 'X';
return board;
}
extend(board, click);
return board;
}
void extend(vector<vector<char>>& board, vector<int>& click)
{
const int row = board.size(), col = board[].size(), rowClick = click[], colClick = click[];
char location = ~;
int count = ;
vector<int> tempClick;
// 计算当前位置的相邻情况,location从左边起 8 位分别表示 右,右上,上,左上,左,左下,下,右下 是否有相邻块
if (colClick % col == col - ) // 右,11000001
location &= ~;
if (rowClick == ) // 上,01110000
location &= ~;
if (colClick % col == ) // 左,00011100
location &= ~;
if (rowClick == row - ) // 下,00000111
location &= ~;
// 统计周围雷数计数,从右开始,逆时针方向搜索
if (location & << && board[rowClick][colClick + ] == 'M')
count++;
if (location & << && board[rowClick - ][colClick + ] == 'M')
count++;
if (location & << && board[rowClick - ][colClick] == 'M')
count++;
if (location & << && board[rowClick - ][colClick - ] == 'M')
count++;
if (location & << && board[rowClick][colClick - ] == 'M')
count++;
if (location & << && board[rowClick + ][colClick - ] == 'M')
count++;
if (location & << && board[rowClick + ][colClick] == 'M')
count++;
if (location & << && board[rowClick + ][colClick + ] == 'M')
count++;
if (count)// 周围有雷,本地为数字,停止搜索
{
board[rowClick][colClick] = count + '';
return;
}
board[rowClick][colClick] = 'B';// 周围无雷,本地为安全区,继续搜索
if (location & << && board[rowClick][colClick + ] == 'E')
extend(board, tempClick = { rowClick, colClick + });
if (location & << && board[rowClick - ][colClick + ] == 'E')
extend(board, tempClick = { rowClick - , colClick + });
if (location & << && board[rowClick - ][colClick] == 'E')
extend(board, tempClick = { rowClick - , colClick });
if (location & << && board[rowClick - ][colClick - ] == 'E')
extend(board, tempClick = { rowClick - , colClick - });
if (location & << && board[rowClick][colClick - ] == 'E')
extend(board, tempClick = { rowClick, colClick - });
if (location & << && board[rowClick + ][colClick - ] == 'E')
extend(board, tempClick = { rowClick + , colClick - });
if (location & << && board[rowClick + ][colClick] == 'E')
extend(board, tempClick = { rowClick + , colClick });
if (location & << && board[rowClick + ][colClick + ] == 'E')
extend(board, tempClick = { rowClick + , colClick + });
return;
}
};
● 大佬的代码,38 ms,深度优先遍历,与后面的广度优先遍历在格式上保持一致
class Solution
{
public:
vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click)
{
int m = board.size(), n = board[].size(), row = click[], col = click[];
int count, i, j, r, c;
vector<int>tempClick;
if (board[row][col] == 'M')
{
board[row][col] = 'X';
return board;
}
for (count = , i = -; i < ; i++)
{
for (j = -; j < ; j++)
{
if (i == && j == )
continue;
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)
{
board[row][col] = (char)(count + '');
return board;
}
for (board[row][col] = 'B', i = -; i < ; i++)
{
for (j = -; j < ; j++)
{
if (i == && j == )
continue;
r = row + i, c = col + j;
if (r < || r >= m || c < || c < || c >= n)
continue;
if (board[r][c] == 'E')
updateBoard(board, tempClick = { r, c });
}
}
return board;
}
};
● 大佬的广度优先遍历,31 ms,最快的解法算法与之相同,但维护一个 unordered_set<int> 用于保存已经访问过的点来防止重复访问
class Solution
{
public:
vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click)
{
const int m = board.size(), n = board[].size();
queue<vector<int>> q;
vector<int> cell;
int row, col, count, i, j, r, c;
for (q.push(click); !q.empty();)
{
cell = q.front(),q.pop();
row = cell[], col = cell[];
if (board[row][col] == 'M')
{
board[row][col] = 'X';
continue;
}
for (count = , i = -; i < ; i++)
{
for (j = -; j < ; j++)
{
if (i == && j == )
continue;
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)
{
board[row][col] = (char)(count + '');
continue;
}
for (board[row][col] = 'B', i = -; i < ; i++)
{
for (j = -; j < ; j++)
{
if (i == && j == )
continue;
r = row + i, c = col + j;
if (r < || r >= m || c < || c < || c >= n)
continue;
if (board[r][c] == 'E')
{
q.push(vector<int>{r, c});
board[r][c] = 'B';
}
}
}
}
return board;
}
};
529. Minesweeper的更多相关文章
- LN : leetcode 529 Minesweeper
lc 529 Minesweeper 529 Minesweeper Let's play the minesweeper game! You are given a 2D char matrix r ...
- Week 5 - 529.Minesweeper
529.Minesweeper Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char ma ...
- 529. Minesweeper扫雷游戏
[抄题]: Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix repre ...
- LeetCode 529. Minesweeper
原题链接在这里:https://leetcode.com/problems/minesweeper/description/ 题目: Let's play the minesweeper game ( ...
- leetcode笔记(七)529. Minesweeper
题目描述 Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix repres ...
- 529 Minesweeper 扫雷游戏
详见:https://leetcode.com/problems/minesweeper/description/ C++: class Solution { public: vector<ve ...
- [LeetCode] 529. Minesweeper 扫雷
Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix representin ...
- LC 529. Minesweeper
Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix representin ...
- 【LeetCode】529. Minesweeper 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetco ...
随机推荐
- 雷林鹏分享:Ruby CGI Sessions
Ruby CGI Sessions CGI::Session 可以为用户和CGI环境保存持久的会话状态,会话使用后需要关闭,这样可以保证数据写入到存储当中,当会话完成后,你需要删除该数据. #!/us ...
- Kubernetes实践--hello world 示例
本文所说的Hello world是一个web留言板应用,并且是基于PHP+Redis的两层分布式架构的web应用,前端PHP web网站通过访问后端Redis数据库完成用户留言的查询和添加功能,具备读 ...
- Python Inotify 监视LINUX文件系统事件
Inotify 可以监视的LINUX文件系统事件包括: --IN_ACCESS,即文件被访问 --IN_MODIFY,文件被write --IN_ATTRIB,文件属性被修改,如chmod.chown ...
- 论integer是地址传递还是值传递(转)
原文链接:http://blog.csdn.net/witsmakemen/article/details/46874717 论integer是地址传递还是值传递 Integer 作为传参的时候是地址 ...
- python多任务的导包问题
多线程的使用: import threading def func(x): print(x) t= threading.Thread(target=func,args=(12,)) # 线程启动 t. ...
- 通过Ftp put命令上传导致文件损坏的解决办法
通过Linux命令行向在一台Windows FTP服务器上传文件.然后在另一台Windows客户机登录FTP服务器下载,但是下载后的文件大小变了,exe文件错误了不能正确执行.刻意打包的文件(.rar ...
- CS231n课程笔记翻译6:神经网络笔记 part1
译者注:本文智能单元首发,译自斯坦福CS231n课程笔记Neural Nets notes 1,课程教师Andrej Karpathy授权翻译.本篇教程由杜客翻译完成,巩子嘉和堃堃进行校对修改.译文含 ...
- 使TextView里面的文字滚动起来
要使TextView里面的内容滚动起来,那么就要配置好几个参数, 1.focusable来获取焦点. 2.ellipsize来获得滚动的方法. 3.focusableInTouchMode来获取触摸方 ...
- 利用U盘大白菜软件来重装win7系统
个人装win7系统用了两个U盘,一个做启动盘(FAT32格式),另外一个当做系统盘(NTFS格式). 首先在电脑里面下载一个大白菜软件,并且安装好,打开软件,插上U盘,检测到了该U盘即可一键制作启动盘 ...
- Java编程之Date的相关操作
一:讲字符串的时间格式数据转换成时间对象 //将字符串的时间数据,转换成时间 String dateString="2007-12-12"; DateFormat date=new ...