题目

NEKO#ΦωΦ has just got a new maze game on her PC!

The game's main puzzle is a maze, in the forms of a \(2×n\) rectangle grid. NEKO's task is to lead a Nekomimi girl from cell \((1,1)\) to the gate at \((2,n)\) and escape the maze. The girl can only move between cells sharing a common side.

However, at some moments during the game, some cells may change their state: either from normal ground to lava (which forbids movement into that cell), or vice versa (which makes that cell passable again). Initially all cells are of the ground type.

After hours of streaming, NEKO finally figured out there are only q such moments: the i-th moment toggles the state of cell \((ri,ci)\) (either from ground to lava or vice versa).

Knowing this, NEKO wonders, after each of the q moments, whether it is still possible to move from cell \((1,1)\) to cell \((2,n)\) andwithout going through any lava cells.

Although NEKO is a great streamer and gamer, she still can't get through quizzes and problems requiring large amount of Brain Power. Can you help her?

\(2×n\)的迷宫,从\((1,1))\)出发到\((2,n)\),初始时全部的都是地面,每次询问会把一个地面给变成熔浆,熔浆变成地面,熔浆不能通过,问是否可以走到。

输入格式

The first line contains integers$ n, q (2≤n≤105, 1≤q≤105)$.

The i-th of q following lines contains two integers \(ri, ci (1≤ri≤2, 1≤ci≤n)\), denoting the coordinates of the cell to be flipped at the i-th moment.

It is guaranteed that cells \((1,1)\) and \((2,n)\) never appear in the query list.

第一行两个整数\(n,q\).

接下来又q行,第\(i\)行包括两个整数,\(ri, ci (1≤ri≤2, 1≤ci≤n)\),表示在第i个时刻要翻转的单元格的坐标.

确保单元格\((1,1)\)和\((2,n)\)永远不会出现在查询列表中。

输出格式

For each moment, if it is possible to travel from cell \((1,1)\) to cell \((2,n)\), print "Yes", otherwise print "No". There should be exactly q answers, one after every update.

对于每个时刻,如果可以从单元格\((1,1)\)到单元格\((2,n)\),则打印"Yes",否则打印"No",有q个答案,每行输入对应一行输出.

输入样例

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

输出样例

Yes
No
No
No
Yes

题解

这道题的大概思路还是挺好想的,大概就是每次输入的时候记录下挡路的"单元"

上图用圈出来的部分都是一个单元,可以阻挡玩家通过,如果整个地图只有一个熔浆单元格,显然是不足以构成一个单元,至少两个(上面三种情况)才可以构成.

那么思路就有了,如果在反转的时候对面三个格子已经有了熔浆单元,单元的数量加一.

在反转的时候,如果对面已经有了熔浆单元,说明这个格子在从地面到熔浆的过程中,单元的数量增加了,那么反转的时候自然要减回去.

还有一个小技巧,为了方便的表示"对面的x坐标",有三种方式:

  1. 使用if语句,例如#define o(x) ((x)==1?2:1),o(x)表示对面的横坐标

  2. 因为\(x\)坐标有\(1\)和\(2\)两种情况,那么用3去减就可以了,同样可以推广,如果一个变量只有两个值的可能性\(a\)和\(b\),使用同一种运算在a和b之间转化的方式就是\(a+b-x\),例如当\(x\)为\(a\),\(a+b-a=b\);当\(x\)为\(b\),\(a+b-b=a\),在这道题中,\(3-x\)表示对面的横坐标.

  3. 有没有感觉这种运算像取反操作?我们只要稍作修改,就可以使用取反来操作.在输入之后立刻x--,然后就可以用!x表示对面的横坐标,这种操作好在可以少开\(10^5\)大小的数组.

代码

#include <cstdio>
int vis[2][100005];
int n, q, x, y, ans;
int main() {
scanf("%d%d", &n, &q);
while (q--) {
scanf("%d%d", &x, &y);
x--;
if (vis[x][y]) {
if (vis[!x][y]) ans--;
if (vis[!x][y + 1] && y + 1 <= n) ans--; //注意边界
if (vis[!x][y - 1] && y - 1 >= 1) ans--;
} else {
if (vis[!x][y]) ans++;
if (vis[!x][y + 1] && y + 1 <= n) ans++;
if (vis[!x][y - 1] && y - 1 >= 1) ans++;
}
vis[x][y] = !vis[x][y];
printf("%s\n", ans ? "No" : "Yes");
}
}

NEKO's Maze Game - Codeforces 题解的更多相关文章

  1. Codeforces Round #614 (Div. 2) C - NEKO's Maze Game

    题目链接:http://codeforces.com/contest/1293/problem/C 题目:给定一个 2*n的地图,初始地图没有岩浆,都可以走, 给定q个询问,每个询问给定一个点(x,y ...

  2. Codeforces 1292A/1293C - NEKO's Maze Game

    题目大意: 有一个2*n的图 NEKO#ΦωΦ要带领mimi们从(1,1)的点走到(2,n)的点 每次会操作一个点,从可以通过到不可以通过,不可以通过到可以通过 每操作一次要回答一次NEKO#ΦωΦ能 ...

  3. Codeforces Round #614 (Div. 1) A. NEKO's Maze Game (思维,模拟)

    题意:有一个\(2\)X\(n\)的矩阵,你想从\((1,1)\)走到\((2,n)\),每次可以向上下左右四个方向走,但在某些时间段某个点会被堵住,如果已经被堵住,那么即恢复正常,每次对某个点操作, ...

  4. 题解 CF1292A 【NEKO's Maze Game】

    有一个结论: 当 \((1,1)\) 不能抵达 \((2,n)\) 时,必定存在一个点对,这两个点的值均为真,且坐标中的 \(x\) 互异,\(y\) 的差 \(\leq 1\) 这个结论的正确性感觉 ...

  5. CodeForces 1292A NEKO's Maze Game(思维)

    #include <stdio.h> #include <string.h> #include <iostream> #include <string> ...

  6. CodeForces 1293 C NEKO's Maze Game

    [题目链接] [题目大意] 有一个2 ∗ n的地图,小女孩从(1,1)想移动到(2,n) 有q次询问,每次询问更改一个格子状态(是否可以通过) 只能上下左右移动而不能斜着移动,问每次操作后,是否可以移 ...

  7. 【迷宫问题】CodeForces 1292A A NEKO's Maze Game

    题目大意 vjudge链接 共两行,从(1,n)到(2,n). 每过一个时刻会有一个位置的状态变化,从能到达这个位置变成不能到达,或从不能到达变成能到达,问在每个时刻中是否能从起点到终点. 数据范围 ...

  8. NEKO's Maze Game

    NEKO#ΦωΦ has just got a new maze game on her PC! The game's main puzzle is a maze, in the forms of a ...

  9. 每日一题:codeforces题解

    题目 B. Peculiar Movie Preferences time limit per test 2 seconds memory limit per test 512 megabytes i ...

随机推荐

  1. 前端每日实战:4# 视频演示如何用纯 CSS 创作一个金属光泽 3D 按钮特效

    效果预览 按下右侧的"点击预览"按钮在当前页面预览,点击链接全屏预览. https://codepen.io/zhang-ou/full/MGeRRO 可交互视频教程 此视频是可以 ...

  2. OC和C++混编需要注意的问题

    文章首发于github.io 2018-12-17 21:01:55 方案一 1. .c文件的identify and type右边栏修改为Objective-C source 2. Built se ...

  3. vue自定义指令要点

    vue自定义指令的基础使用这里就不阐述,看官网文档:https://cn.vuejs.org/v2/guide/custom-directive.html 本文用一个实例描述自定义指令的要点,自定义一 ...

  4. C# 存储相同键多个值的Dictionary

    涉及到两个问题: 一.访问磁盘中文件夹.文件夹下面的文件夹 先看一下磁盘文件夹结构 C盘下面有个根文件夹SaveFile,SaveFIle下面有两个子文件夹分别为,2018.2019, 子文件下201 ...

  5. Java 抽象类 抽象方法 使用说明

    知识点 什么是抽象类 抽象类与普通类主要两点不同: 1.在类的修饰符后面多了一个abstract关键字 2.抽象类是不允许通过new来实例化的 由于抽象类不能通过new来实例化,所以基本上是在继承中当 ...

  6. docker 升级后启动异常处理

    docker升级后启动时提示如下错误: Unable to create at Docker.Core.Pipe.NamedPipeClient.d__5.MoveNext() --- End of ...

  7. MATLAB神经网络(4) 神经网络遗传算法函数极值寻优——非线性函数极值寻优

    4.1 案例背景 \[y = {x_1}^2 + {x_2}^2\] 4.2 模型建立 神经网络训练拟合根据寻优函数的特点构建合适的BP神经网络,用非线性函数的输入输出数据训练BP神经网络,训练后的B ...

  8. 通过CGAL将一个多边形剖分成Delaunay三角网

    目录 1. 概述 2. 实现 3. 结果 4. 参考 1. 概述 对于平面上的点集,通过Delaunay三角剖分算法能够构建一个具有空圆特性和最大化最小角特性的三角网.空圆特性其实就是对于两个共边的三 ...

  9. js 数组 方法

    instanceof 检测一个对象是否是数组;(用来对付复杂数据类型;)// 简单数据类型 typeof ;A instanceof B // A是不是B造出来的;例: var arr = [1,2, ...

  10. mybatis返回自增主键踩坑记

    背景 MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以对配置和原生Map ...