CF 1292【Div.1】A、CF 1293【Div.2】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 n, q n,q ( 2 ≤ n ≤ 10 (2≤n≤10 (2n105, 1 ≤ q ≤ 10 1≤q≤10 1q105 ) . ). ).

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

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


Output

For each moment, if it is possible to travel from cell ( 1 , 1 ) (1,1) (1,1) to cell ( 2 , n ) , (2,n), (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).


input

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


output

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 ) . (1,1)→(1,2)→(1,3)→(1,4)→(1,5)→(2,5). (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 ) (1,3) (1,3).
After the fourth query, the ( 2 , 3 ) (2,3) (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 2*n 2n的地图,小女孩从 ( 1 , 1 ) (1,1) (1,1)想移动到 ( 2 , n ) (2,n) (2,n),有 k k k次询问,每次询问更改一个格子状态(是否可以通过),只能上下左右移动而不能斜着移动,问每次操作后,是否可以移动到 ( 2 , n ) (2,n) (2,n)

题解

  • 由于题目范围1e5,询问1e5,显然不能暴力直接搜
  • 不能通过只有三种情况,按照墙处理。每次操作之后 c h e c k check check一下,加减墙数,最后判断是否有墙即可

AC-Code

#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e5;
bool ma[3][maxn + 7];
int num;
void check(int x, int y) {
	int o;
	if (x == 1) o = 2;
	else o = 1;
	if (ma[x][y] == false) {// 加墙
		if (ma[o][y] == true)
			++num;
		if (ma[o][y - 1] == true)
			++num;
		if (ma[o][y + 1] == true)
			++num;
	}
	else if (ma[x][y] == true) {// 减墙
		if (ma[o][y] == true)
			--num;
		if (ma[o][y - 1] == true)
			--num;
		if (ma[o][y + 1] == true)
			--num;
	}
	ma[x][y] = !ma[x][y];
}
int main() {
	int n, m;
	while (cin >> n >> m) {
		num = 0;
		memset(ma, false, sizeof ma);
		while (m--) {
			int x, y;
			cin >> x >> y;
			check(x, y);
			if (!num)
				cout << "YES" << endl;
			else
				cout << "NO" << endl;
		}
	}
	return 0;
}

官方题解

#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>
using namespace std;

#define endl '\n'

int n, q;
vector<vector<int>> lava;

void Input() {
	cin >> n >> q;
	lava.resize(2, vector<int>(n, 0));
}

void Solve() {
	int blockedPair = 0;
	while (q--) {
		int x, y; cin >> x >> y; x--; y--;
		int delta = (lava[x][y] == 0) ? +1 : -1;

		lava[x][y] = 1 - lava[x][y];
		for (int dy=-1; dy<=1; dy++) {
			if (y+dy < 0 || y+dy >= n) continue;

			if (lava[1-x][y+dy] == 1) blockedPair += delta;
		}

		cout << ((blockedPair == 0) ? "Yes\n" : "No\n");
	}
}

int main(int argc, char* argv[]) {
	ios_base::sync_with_stdio(0); cin.tie(NULL);
	Input(); Solve(); return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值