▶ 扫雷的扩展判定。已知棋盘上所有点的情况(雷区 '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的更多相关文章

  1. LN : leetcode 529 Minesweeper

    lc 529 Minesweeper 529 Minesweeper Let's play the minesweeper game! You are given a 2D char matrix r ...

  2. Week 5 - 529.Minesweeper

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

  3. 529. Minesweeper扫雷游戏

    [抄题]: Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix repre ...

  4. LeetCode 529. Minesweeper

    原题链接在这里:https://leetcode.com/problems/minesweeper/description/ 题目: Let's play the minesweeper game ( ...

  5. leetcode笔记(七)529. Minesweeper

    题目描述 Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix repres ...

  6. 529 Minesweeper 扫雷游戏

    详见:https://leetcode.com/problems/minesweeper/description/ C++: class Solution { public: vector<ve ...

  7. [LeetCode] 529. Minesweeper 扫雷

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

  8. LC 529. Minesweeper

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

  9. 【LeetCode】529. Minesweeper 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetco ...

随机推荐

  1. 使用AspNetPager进行分页,查询条件丢失问题

    在Asp.Net中使用AspNetPager进行分页时,发现一个问题: 当通过查询条件进行查询后,对查询结果进行翻页操作时,查询条件会丢失. 当修改UrlPaging属性后(设置UrlPaging=“ ...

  2. Java基础——概述

    body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...

  3. 【python】利用scipy进行层次聚类

    参考博客: https://joernhees.de/blog/2015/08/26/scipy-hierarchical-clustering-and-dendrogram-tutorial/ 层次 ...

  4. APP界面框架初窥---标签导航

    标签导航是十大界面框架设计里最常用的界面框架设计,也是被业界之内公认的一种普遍使用的页面框架设计.那么这种页面框架设计在作业方面对一个用户来说也是最常见的一种页面框架设计,比如说微博.微信.手机百度. ...

  5. OK335xS pwm buzzer Linux driver hacking

    /**************************************************************************** * OK335xS pwm buzzer L ...

  6. CI框架的引导流程以及基准测试类

    一[CI]框架的引导流程了解一下,并掌握如何新增自己的扩展类库   http://www.cnblogs.com/ohmygirl/p/CIRead-4.html // CI框架源码阅读笔记4 引导文 ...

  7. 对MYSQL慢查询slow query 日志记录内容的疑惑

    初始:由于新装服务器出现CPU占用过高,响应不及时的问题排查,因为环境基于最基础的LAMP构架 想到开启 MYSQL slow_query_log 慢查询日志做原因分析: 但是看到日志内容之后有点茫然 ...

  8. springboot结合开源editor.md集成markdonw编辑器

    今天来实现一个简单的功能,通常blog后台编辑大多使用的是富文本编辑器,比如百度的Ueditor,比较轻巧的wangEditor,那么如何使用开源editor.md的markdown呢? 搭建一个sp ...

  9. opencv之批量转换灰度图并保存

    当图片名字有数字规律时,批量处理方式. ①srcImage 图片名字有规律 ②将srcImage文件下的图片,转换为灰度图并保存入grayImage文件夹. ③ #include <iostre ...

  10. [TopCoder12141]SweetFruits

    vjudge description 有\(n\)个水果,有一些水果是香的,它们各有一个香度值,剩下的水果是不香的. 现在你要把这\(n\)个水果连成一棵树,定义一个水果是真香的当且仅当它是香的且他与 ...