今天做了一个非常简单但是却难到我的题TAT,用循环搜索改了好几遍,会超时,后来看别人的题解,我想的太复杂了,才发现这个题真的很巧妙。
https://codeforces.com/problemset/problem/1292/A
题意
给一个2*n的矩形迷宫,玩家需要从(1,1)走到(2,n),在游戏中的每个时刻,会在一个位置(r,c)上生成岩浆,或者把岩浆改为能通过的路。求在每一个时刻过后是否可以从(1,1)走到(2,n)。
思路
每个位置的初始状态设为0,表示可以通过,1表示岩浆不可以通过。num记录阻塞对数量。每将一个位置(r,c)改为岩浆之后,只需看另一列的相邻三个位置(r’,c-1), (r’,c), (r’,c+1)是否有岩浆,有一个岩浆则num++,阻塞对加一。同理,每将一个位置改为路之后,也看另一列三个位置是否有岩浆,有一个则num- -。
每个时刻结束后,只需判断num,num==0则输出yes,否则输出no。
AC代码
#include <iostream>
#include <cstdio>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn = 1e5 + 10;
bool a[3][maxn];
int main()
{
int n, q, b, c;
int num = 0;
cin >> n >> q;
while (q--)
{
cin >> b >> c;
if (a[b][c] == 0)
{
a[b][c] = 1;
if (a[3 - b][c - 1]) num++;
if (a[3 - b][c]) num++;
if (a[3 - b][c + 1]) num++;
}
else {
a[b][c] = 0;
if (a[3 - b][c - 1]) num--;
if (a[3 - b][c]) num--;
if (a[3 - b][c + 1]) num--;
}
if (num == 0) cout << "Yes" << endl;
else cout << "No" << endl;
}
return 0;
}