A. Vika and Her Friends
A. Vika and Her Friends
Vika and her friends went shopping in a mall, which can be represented as a rectangular grid of rooms with sides of length and . Each room has coordinates , where . Thus we call a hall with coordinates a neighbouring for it if .
Tired of empty fashion talks, Vika decided to sneak away unnoticed. But since she hasn't had a chance to visit one of the shops yet, she doesn't want to leave the mall. After a while, her friends noticed Vika's disappearance and started looking for her.
Currently, Vika is in a room with coordinates , and her friends are in rooms with coordinates , , ... , respectively. The coordinates can coincide. Note that all the girls must move to the neighbouring rooms.
Every minute, first Vika moves to one of the adjacent to the side rooms of her choice, and then each friend (seeing Vika's choice) also chooses one of the adjacent rooms to move to.
If at the end of the minute (that is, after all the girls have moved on to the neighbouring rooms) at least one friend is in the same room as Vika, she is caught and all the other friends are called.
Tell us, can Vika run away from her annoying friends forever, or will she have to continue listening to empty fashion talks after some time?
Input
Each test consists of multiple test cases. The first line contains a single integer () — the number of test cases. The description of the test cases follows.
The first line of each test case contains three integers , , () — the sizes of the mall and the number of Vika's friends.
The second line of each test case contains a pair of integers and (, ) — the coordinates of the room where Vika is.
Each of the next lines of each test case contains a pair of integers and (, ) — the coordinates of the room where the -th friend is.
Output
For each test case, output "YES" if Vika can run away from her friends forever, otherwise output "NO".
You can output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be accepted as a positive answer.
Example
input
6 2 2 1 1 1 1 2 2 2 2 1 1 2 2 2 2 1 2 1 1 1 1 2 5 5 4 3 3 1 1 1 5 5 1 5 5 2 2 2 1 1 2 1 1 2 3 4 1 1 2 3 3
output
YES
NO
YES
NO
YES
YES
Note
In the first test case, the friend will never catch up with Vika, because Vika can always move to the room diagonally opposite to the one where the friend is.
In the second test case, no matter where Vika goes, each of her friends can catch her after the first move.
In the third test case, Vika and her friend will always be in different halls.
解题思路
不妨先把的棋盘染成黑白相间的形式,如果为偶数则染白色,否则染黑色,如下图:
先给出结论,如果所有的都与的格子颜色不同,那么在移动的过程中任意一个都无法与相遇,否则(即存在至少一个的格子颜色与相同)必然存在一个与相遇。
首先如果与的格子颜色不同,那么在一次移动后格子颜色还是不同的(白变黑,黑变白),因此这两个格子永远无法相遇。
然后再证明如果与的格子颜色相同,那么在移动的过程中一定会与相遇。定义与相对位置的面积为这两个点所构成的矩形面积,如下图:
当移动后,总是沿着使得矩形面积减少或不变的方向移动,由于棋盘的大小有限,因此不会存在总是使得面积不变的移动方案,即必然会存在使得面积减少的方案,因此在移动的过程中矩形面积呈非递增,最后减少到,即两个格子会相遇。
AC代码如下:
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 void solve() { 5 int n, x, y; 6 scanf("%*d %*d %d %d %d", &n, &x, &y); 7 bool flag = true; 8 while (n--) { 9 int a, b; 10 scanf("%d %d", &a, &b); 11 if ((x + y & 1) == (a + b & 1)) flag = false; 12 } 13 printf("%s\n", flag ? "YES" : "NO"); 14 } 15 16 int main() { 17 int t; 18 scanf("%d", &t); 19 while (t--) { 20 solve(); 21 } 22 23 return 0; 24 }
参考资料
Codeforces Round #885 (Div.2) Editorial:https://codeforces.com/blog/entry/118333
本文来自博客园,作者:onlyblues,转载请注明原文链接:https://www.cnblogs.com/onlyblues/p/17560404.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效