[LeetCode][沙雕氵]如何用LeetCode130写一个故事?

Code算是程序员的基本功,但是有些时候,code还是有那么一点无聊的,因而总是希望给自己找些乐子。今天在写LeetCode130的时候就突然来了一点点有趣的想法。
首先先来看一下题面:
题面
这是一个非常简单的题,我们可以用deque+set轻松的解决他,但是~
各位看官,不觉得我们可以用这个题写一个故事吗
在末日,我们爆发了尸潮,凶残的僵尸包围住了民众,此时英勇的救援队出现了:

deque<location> rescueTeam;// using loaction = pair<int, int>

我们需要为它募集成员,谁是一开始的成员呢?当然是在外围,而且没有被感染的人们:

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值