Description

The 2D-Nim board game is played on a grid, with pieces on the grid points. On each move, a player may remove any positive number of contiguous pieces in any row or column. The player who removes the last piece wins. For example, consider the left grid in the following figure. 
 
The player on move may remove (A), (B), (A, B), (A, B, C), or (B,F), etc., but may not remove (A, C), (D, E), (H, I) or (B, G). 
For purposes of writing 2D-Nim-playing software, a certain programmer wants to be able to tell whether or not a certain position has ever been analyzed previously. Because of the rules of 2D-Nim, it should be clear that the two boards above are essentially equivalent. That is, if there is a winning strategy for the left board, the same one must apply to the right board. The fact that the contiguous groups of pieces appear in different places and orientations is clearly irrelevant. All that matters is that the same clusters of pieces (a cluster being a set of contiguous pieces that can be reached from each other by a sequence of one-square vertical or horizontal moves) appear in each. For example, the cluster of pieces (A, B, C, F, G) appears on both boards, but it has been reflected (swapping left and right), rotated, and moved. Your task is to determine whether two given board states are equivalent in this sense or not.

Input

The first line of the input file contains a single integer t (1 ≤ t ≤ 10), the number of test cases, followed by the input data for each test case. The first line of each test case consists of three integers W, H, and n (1 ≤ W, H ≤ 100). W is the width, and H is the height of the grid in terms of the number of grid points. n is the number of pieces on each board. The second line of each test case contains a sequence of n pairs of integers xi , yi, giving the coordinates of the pieces on the first board (0 ≤ xi < W and 0 ≤ yi < H). The third line of the test case describes the coordinates of the pieces on the second board in the same format.

Output

Your program should produce a single line for each test case containing a word YES or NO indicating whether the two boards are equivalent or not.

Sample Input

2
8 5 11
0 0 1 0 2 0 5 0 7 0 1 1 2 1 5 1 3 3 5 2 4 4
0 4 0 3 0 2 1 1 1 4 1 3 3 3 5 2 6 2 7 2 7 4
8 5 11
0 0 1 0 2 0 5 0 7 0 1 1 2 1 5 1 3 3 6 1 4 4
0 4 0 3 0 2 1 1 1 4 1 3 3 3 5 2 6 2 7 2 7 4

Sample Output

YES
NO

Source

Tehran 2002, First Iran Nationwide Internet Programming Contest
 
    题目大致意思:就是两张图啦,然后有一个相消规则,再给你一个图,问你这两个图等价不?
    分析:
    特别水的题,不仅题目水,测试数据更水,无力吐槽。
    讲一下做的方法:
    1.既然是要等价的话,那么对于某个点,它的操作方法在另一张图上对应的点操作方法和操作方法数肯定是一样的。
    2.而且题目说了,只能消连续相邻的点
    3.既然如此,那么对应的点的连续相邻的点的个数一定要相同,不然要是缺一个或者多一个,那么操作的方法和次数都会不一样
    4.那么这题目就转化成了求连续相邻点的个数的问题了,是不是很水
    5.不过最后的连续相邻点个数的数组要排一次序
 
代码如下:
#include <iostream>

using namespace std;

bool map[][];
int W, H, n; struct dot
{
int x, y;
}dots[]; int dot1[], dot2[]; void quicksort(int left, int right, int *dotx)
{
int i, j, temp;
if (left < right)
{
i = left, j = right, temp = dotx[left];
while (i < j)
{
while (i < j&&dotx[j] >= temp) j--;
dotx[i] = dotx[j];
while (i < j&&dotx[i] <= temp) i++;
dotx[j] = dotx[i];
}
dotx[i] = temp;
quicksort(left, j - , dotx);
quicksort(j + , right, dotx);
}
} void Count(int *dot, int i)
{
int x, y, sum;
sum = ;
x = dots[i].x;
y = dots[i].y;
y--;
while (map[x][y] && y >= ) //统计左边点的个数
{
sum++;
y--;
}
y = dots[i].y;
y++;
while (map[x][y] && y < H) //统计右边点的个数
{
sum++;
y++;
}
y = dots[i].y;
x--;
while (map[x][y] && x >= ) //统计下面点的个数
{
sum++;
x--;
}
x = dots[i].x;
x++;
while (map[x][y] && x < W) //统计上面点的个数
{
sum++;
x++;
}
dot[i] = sum;
} int main()
{
int t;
cin >> t;
int sum1, sum2;
while (t--)
{
sum1 = sum2 = ;
memset(map, false, sizeof(map));
cin >> W >> H >> n;
for (int i = ; i <= n; i++) //输入第一组点
{
cin >> dots[i].x >> dots[i].y;
map[dots[i].x][dots[i].y] = true;
}
for (int i = ; i <= n; i++)
Count(dot1, i), sum1 += dot1[i]; //第一张图的连续点数
memset(map, false, sizeof(map));
for (int i = ; i <= n; i++) //输入第二组点
{
cin >> dots[i].x >> dots[i].y;
map[dots[i].x][dots[i].y] = true;
}
for (int i = ; i <= n; i++)
Count(dot2, i), sum2 += dot2[i]; //第二张图的连续点数
if (sum1 != sum2) cout << "NO" << endl;
else
{
quicksort(, n, dot1);
quicksort(, n, dot2);
int flag = ;
for (int i = ; i <= n; i++)
{
if (dot1[i] != dot2[i])
{
//我之前在这里写了输出用来看数据的
//我提交的时候忘记删了,结果还对了
//不得不说这测试数据是真的水
flag = ;
break;
}
}
if (flag) cout << "YES" << endl;
else cout << "NO" << endl;
}
}
}

POJ 1021 2D-Nim的更多相关文章

  1. Georgia and Bob POJ - 1704 阶梯Nim

    $ \color{#0066ff}{ 题目描述 }$ Georgia and Bob decide to play a self-invented game. They draw a row of g ...

  2. poj 1021矩阵平移装换后是否为同一个矩阵

    2D-Nim Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3081   Accepted: 1398 Descriptio ...

  3. POJ 1704 Staircase Nim 阶梯博弈

    #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int ...

  4. POJ 1021 人品题

    报告见代码.. #include <iostream> #include <cstdio> #include <cstring> #include <algo ...

  5. 一位学长的ACM总结(感触颇深)

    发信人: fennec (fennec), 信区: Algorithm 标 题: acm 总结 by fennec 发信站: 吉林大学牡丹园站 (Wed Dec 8 16:27:55 2004) AC ...

  6. 狗狗40题~ (Volume C)

    A - Triangles 记忆化搜索呗.搜索以某三角形为顶的最大面积,注意边界情况. #include <stdio.h> #include <cstring> #inclu ...

  7. 【POJ】【2068】Nim

    博弈论/DP 这是Nim?这不是巴什博奕的变形吗…… 我也不会捉啊,不过一看最多只有20个人,每人最多拿16个石子,总共只有8196-1个石子,范围好像挺小的,嗯目测暴力可做. so,记忆化搜索直接水 ...

  8. 【POJ】【2975】Nim

    博弈论 我哭……思路错误WA了6次?(好像还有手抖点错……) 本题是要求Nim游戏的第一步必胜策略有几种. 一开始我想:先全部异或起来得到ans,从每个比ans大的堆里取走ans个即可,答案如此累计… ...

  9. POJ 1704 Georgia and Bob (Nim游戏变形)

    题目:http://poj.org/problem?id=1704 思路:Nim游戏策略,做如下转换,如果N是偶数,则两两配对,将两个数之间的格子数(距离)看做成这一堆石头的数量. 如果N是奇数,则将 ...

随机推荐

  1. 你知道织梦后台安装插件时为什么会出现这个Character postion 686, 'item'&n

    https://zhidao.baidu.com/question/589525064.html?qbl=relate_question_3&word=Tag Character postio ...

  2. 真正从零开始,TensorFlow详细安装入门图文教程!

    本文转载地址:https://www.leiphone.com/news/201606/ORlQ7uK3TIW8xVGF.html AI这个概念好像突然就火起来了,年初大比分战胜李世石的AlphaGo ...

  3. 强大的Cmder

    why 漂亮,包装并美化了各个shell 带task功能,能记忆,能执行脚本 配合win10的bash,能实现类似xshell的功能 注意点 需要注意的一点,Cmder来源于另外一个项目ConEmu, ...

  4. ELK入门级介绍--打造实时日志查询系统

    这几天一直在研究ElasticSearch,在网上看到一篇好的文章和大家分享. ELK平台介绍 在搜索ELK资料的时候,发现这篇文章比较好,于是摘抄一小段: 以下内容来自:http://baidu.b ...

  5. maven系列--settings.xml

    安装maven,会有一份settings.xml文件,我们可以理解这份文件就是用来管理本地maven的一系列东西的.这里我先贴出一份完整的原始xml. <?xml version="1 ...

  6. 通过反编译深入理解Java String及intern(转)

    通过反编译深入理解Java String及intern 原文传送门:http://www.cnblogs.com/paddix/p/5326863.html 一.字符串问题 字符串在我们平时的编码工作 ...

  7. 【Java SE】如何用Java实现插入排序

    摘要:前面三期分别写了三篇简单排序的算法,今天来讲一点稍微难一点的排序算法-----插入排序. 基本思想: 设n个数据已经按照顺序排列好(假定从小排到大). 输入一个数据x,将其放在恰当的位置,使其顺 ...

  8. promise使用方法

    代码报错会走向失败时的回调 let p = new Promise((resolve, reject) => { throw new Error("我错了"); //会失败 ...

  9. CentOS 7修改网卡名称

    CentOS 7 修改网卡名为eth0 标签: linux 笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 CentOS7的网卡名称太长,这不符合我们的使用习惯, ...

  10. MyCat 入门:漫谈 MyCat 配置系统

    文章首发于[博客园-陈树义],点击跳转到原文<MyCat 入门:漫谈 MyCat 配置系统> 上篇文章<MyCat 启蒙:分布式系统的数据库架构演变>中,我们通过一个项目从零到 ...