C. 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 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) without 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?

Input
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.

Output
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.

You can print the words in any case (either lowercase, uppercase or mixed).

Example
inputCopy

5 5
2 3
1 4
2 4
2 3
1 4
outputCopy
Yes
No
No
No
Yes
Note
We’ll crack down the example test here:

After the first query, the girl still able to reach the goal. One of the shortest path ways should be: (1,1)→(1,2)→(1,3)→(1,4)→(1,5)→(2,5).
After the second query, it’s impossible to move to the goal, since the farthest cell she could reach is (1,3).
After the fourth query, the (2,3) is not blocked, but now all the 4-th column is blocked, so she still can’t reach the goal.
After the fifth query, the column barrier has been lifted, thus she can go to the final goal again.

题意:给你一个2*n矩阵格子,然后有q个询问,每次询问给出一个坐标,如果该坐标上没有障碍物,那就给它加上。如果有了,那就给它去掉。对于每个询问是否能从(1,1)走到(2,n)? 能输出Yes,否则No;

解析:怎么样去判断不能走通的呢? 如果当前点在第二行,那么它的上一个和左右斜对角两个只要有一个是障碍物,那一定走不通的
例如
在这里插入图片描述

或者:
在这里插入图片描述
所以只要存在这种情况我们就res++;
反之如果这个点障碍物被清了,和它相关的res–

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+100;
int a[3][N],x,y;
int n,q;
int main()
{
	cin>>n>>q;
	int res=0;
	for(int i=1;i<=q;i++)
	{
		cin>>x>>y;
		if(a[x][y]==0)
		{
			a[x][y]=1;
			for(int i=-1;i<=1;i++)
			{
				if(a[3-x][y+i]==1) res++;
			}
		}
		else
		{
			a[x][y]=0;
			for(int i=-1;i<=1;i++)
			{
				if(a[3-x][y+i]==1) res--;
			}
		}
		if(res==0 )puts("Yes");
		else puts("No"); 
	}
}


发布了309 篇原创文章 · 获赞 6 · 访问量 5268

猜你喜欢

转载自blog.csdn.net/qq_43690454/article/details/104071236