CodeForces 1293 C NEKO's Maze Game

【题目链接】

【题目大意】

有一个2 ∗ n的地图,小女孩从(1,1)想移动到(2,n)

有q次询问,每次询问更改一个格子状态(是否可以通过)

只能上下左右移动而不能斜着移动,问每次操作后,是否可以移动到(2,n)

【Input】

第一行n,q (数据范围1e5)

2 * n表示图的大小,q表示更改次数

以下q行,每行输入x,y表示更改点的坐标

【Output】

"Yes" or "No"

【Example】

  input

  5 5
  2 3
  1 4
  2 4
  2 3
  1 4

  

  output

  Yes

  No

  No

  No

  Yes

【思路&分析】

  老规矩,一切题目从暴力开始想

  显然的,每次更新完用dfs暴搜

  理想很丰满,现实很骨干

  一看1 e 5, 一切都玩完

  咳咳——换思路

  仔细审题

  我们发现—这个图是2 * n的

  那么对于每个格子的两种状态,分类讨论      

    • 没有障碍物,可以通过(√)
    • 有障碍物,我们可以选择绕行  

       

     如图所示,红色表示不可通过,蓝色表示可通过

     若出现了红色部分,我们只需要判断其对面三个格子(图示第二行三个蓝格子)中有几个可通过

     但凡有一个不可通过,则“No”

【代码实现】

  需要二维数组保存状态

  需要计数器数可以通行的格子

  

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

inline int read(){
    int x = 0, w = 1;
    char ch = getchar();
    for(; ch > '9' || ch < '0'; ch = getchar()) if(ch == '-') w = -1;
    for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
    return x * w;
}

const int maxn = 1000005;

int n,q;
bool a[3][maxn];
int ans;

inline void check(int x, int y){
    int tmp = 0;
    if(x == 1) tmp = 2;
    else tmp = 1;
    if(a[x][y] == 0){
        if(a[tmp][y - 1]) ans++;
        if(a[tmp][y]) ans++;
        if(a[tmp][y + 1]) ans++;
    }
    else if(a[x][y] == 1){
        if(a[tmp][y - 1]) ans--;
        if(a[tmp][y]) ans--;
        if(a[tmp][y + 1]) ans--;
    }
    a[x][y] = !a[x][y];
}

int main(){
    n = read(), q = read();
    while(q--){
        int x = read(), y = read();
        check(x, y);
        if(!ans) cout << "Yes
";
        else cout << "No
";
    }
    return 0;
}
Code
风吹过,我来过~
原文地址:https://www.cnblogs.com/rui-4825/p/12629454.html