Code算是程序员的基本功,但是有些时候,code还是有那么一点无聊的,因而总是希望给自己找些乐子。今天在写LeetCode130的时候就突然来了一点点有趣的想法。
首先先来看一下题面:
这是一个非常简单的题,我们可以用deque+set轻松的解决他,但是~
各位看官,不觉得我们可以用这个题写一个故事吗
在末日,我们爆发了尸潮,凶残的僵尸包围住了民众,此时英勇的救援队出现了:
deque<location> rescueTeam;// using loaction = pair
我们需要为它募集成员,谁是一开始的成员呢?当然是在外围,而且没有被感染的人们:
for(int i = 0; i < rows; ++i){
for(int j = 0; j < cols; ++j){
if((!i || !j || i == rows - 1 || j == cols - 1) && board[i][j] == 'O')
rescueTeam.push_back({
i, j});
然后嘛,我们需要有一个丧尸题材里面常见的设备——安全屋
set<location> safeHouse;
接下来,救援队需要去救援,很明显大家都不是什么一拳超人,所以,在救援了自己身边的人之后,马上撤回安全屋,由被救出去的民众继续救人,把勇气传递下去:
while(!rescueTeam.empty()){
location rescuer = rescueTeam.front(); rescueTeam.pop_front();
if(safeHouse.find({
rescuer.first - 1, rescuer.second}) == safeHouse.end())
if(rescuer.first - 1 >= 0 && board[rescuer.first - 1][rescuer.second] == 'O')
rescueTeam.push_back({
rescuer.first - 1, rescuer.second});
if(safeHouse.find({
rescuer.first + 1, rescuer.second}) == safeHouse.end())
if(rescuer.first + 1 < rows && board[rescuer.first + 1][rescuer.second] == 'O')
rescueTeam.push_back({
rescuer.first + 1, rescuer.second});
if(safeHouse.find({
rescuer.first, rescuer.second - 1}) == safeHouse.end())
if(rescuer.second - 1 >= 0 && board[rescuer.first][rescuer.second - 1] == 'O')
rescueTeam.push_back({
rescuer.first, rescuer.second - 1});
if(safeHouse.find({
rescuer.first, rescuer.second + 1}) == safeHouse.end())
if(rescuer.second + 1 < cols && board[rescuer.first][rescuer.second + 1] == 'O')
rescueTeam.push_back({
rescuer.first, rescuer.second + 1});
safeHouse.insert(rescuer);
}
当然,并不是所有的事情都会有一个浪漫的结局,没能被救援的人还是无法逃脱变为丧尸的命运,rip
for(int i = 0; i < rows; ++i)
for(int j = 0; j < cols; ++j)
if(safeHouse.find({
i, j}) == safeHouse.end())
board[i][j] = 'X';
最后是完整的故事:
class Solution {
using location = pair<int, int>;
public:
void solve(vector<vector<char>>& board) {
deque<location> rescueTeam;
set<location> safeHouse;
int rows = board.size();
if (!rows) return;
int cols = board[0].size();
for (int i = 0; i < rows; ++i)
for (int j = 0; j < cols; ++j)
if ((!i || !j || i == rows - 1 || j == cols - 1) && board[i][j] == 'O')
rescueTeam.push_back({
i, j });
while (!rescueTeam.empty()) {
location rescuer = rescueTeam.front(); rescueTeam.pop_front();
safeHouse.insert(rescuer);
if (safeHouse.find({
rescuer.first - 1, rescuer.second }) == safeHouse.end())
if (rescuer.first - 1 >= 0 && board[rescuer.first - 1][rescuer.second] == 'O')
rescueTeam.push_back({
rescuer.first - 1, rescuer.second });
if (safeHouse.find({
rescuer.first + 1, rescuer.second }) == safeHouse.end())
if (rescuer.first + 1 < rows && board[rescuer.first + 1][rescuer.second] == 'O')
rescueTeam.push_back({
rescuer.first + 1, rescuer.second });
if (safeHouse.find({
rescuer.first, rescuer.second - 1 }) == safeHouse.end())
if (rescuer.second - 1 >= 0 && board[rescuer.first][rescuer.second - 1] == 'O')
rescueTeam.push_back({
rescuer.first, rescuer.second - 1 });
if (safeHouse.find({
rescuer.first, rescuer.second + 1 }) == safeHouse.end())
if (rescuer.second + 1 < cols && board[rescuer.first][rescuer.second + 1] == 'O')
rescueTeam.push_back({
rescuer.first, rescuer.second + 1 });
}
for (int i = 0; i < rows; ++i)
for (int j = 0; j < cols; ++j)
if (safeHouse.find({
i, j }) == safeHouse.end())
board[i][j] = 'X';
return;
}
};
时间beat60,空间beat100,算是一个可取的方法吧XD