2D-Nim
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 4136   Accepted: 1882

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

总结

就是连通性和图的同构判断。

分析

找出属于同一组的点很简单,DFS就可以搞定。图的同构可以用图的Hash来判断。这个不是我想出来的,是网上看来的:
∑i,]distance(pi,pj)∑i,]distance(pi,pj)
即同一组中所有点的距离加起来,这个数值做为这个图的哈希值。

/*
author:tonygsw
data:2018.8.7
account:zj1228
link:http://poj.org/problem?id=1020
*/
#define ll long long
#define IO ios::sync_with_stdio(false) #include<map>
#include<queue>
#include<math.h>
#include<vector>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
class Node{
public:
int x,y;
};
Node node[];int hash1[],hash2[];
int matri[][];bool vis[][];
int w,h,num,len,len1,len2;int to[][]={,,-,,,,,-};
void init()
{
memset(matri,,sizeof(matri));
memset(vis,,sizeof(vis));
len1=len2=;
}
bool judge(int x,int y)
{
if(x>=&&x<w&&y>=&&y<h&&matri[x][y]&&(!vis[x][y]))
return true;
return false;
}
int dis(Node a,Node b)
{
return pow(a.x-b.x,)+pow(a.y-b.y,);
}
void bfs(int x,int y,int ju)
{
Node beg,nex;len=;
queue<Node>way;
beg.x=x,beg.y=y;
way.push(beg);
vis[x][y]=;
node[len++]=beg;
while(!way.empty())
{
beg=way.front();
way.pop();
for(int i=;i<;i++)
{
nex.x=beg.x+to[i][];
nex.y=beg.y+to[i][];
if(judge(nex.x,nex.y))
{
way.push(nex);
vis[nex.x][nex.y]=;
node[len++]=nex;
}
}
}
for(int i=;i<len;i++)
for(int j=;j<len;j++)
{
if(ju)hash1[len1++]=dis(node[i],node[j]);
else hash2[len2++]=dis(node[i],node[j]);
}
}
bool ans()
{
if(len1!=len2)return false;
else
{
sort(hash1,hash1+len1);
sort(hash2,hash2+len2);
for(int i=;i<len1;i++)
{
if(hash1[i]!=hash2[i])return false;
}
return true;
}
}
int main()
{
int t;
scanf("%d",&t);int x,y;
while(t--)
{
init();
scanf("%d%d%d",&w,&h,&num);
for(int i=;i<num;i++)
{
scanf("%d%d",&x,&y);
matri[x][y]=;
}
for(int i=;i<w;i++)
for(int j=;j<h;j++)
if(matri[i][j]&&(!vis[i][j]))
bfs(i,j,);
memset(matri,,sizeof(matri));
memset(vis,,sizeof(vis));
for(int i=;i<num;i++)
{
scanf("%d%d",&x,&y);
matri[x][y]=;
}
for(int i=;i<w;i++)
for(int j=;j<h;j++)
if(matri[i][j]&&(!vis[i][j]))
bfs(i,j,); if(ans())cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return ;
}
/*
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
*/

poj-1021--2D-Nim--点阵图同构的更多相关文章

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

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

  2. Georgia and Bob POJ - 1704 阶梯Nim

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

  3. POJ 1704 Staircase Nim 阶梯博弈

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

  4. POJ 1021 2D-Nim

    Description The 2D-Nim board game is played on a grid, with pieces on the grid points. On each move, ...

  5. POJ 1021 人品题

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

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

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

  7. 狗狗40题~ (Volume C)

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

  8. Topographic ICA as a Model of Natural Image Statistics(作为自然图像统计模型的拓扑独立成分分析)

    其实topographic independent component analysis 早在1999年由ICA的发明人等人就提出了,所以不算是个新技术,ICA是在1982年首先在一个神经生理学的背景 ...

  9. 【POJ】【2068】Nim

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

  10. 【POJ】【2975】Nim

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

随机推荐

  1. Java thread(4)

    这一块主要是讨论关于进程同步的相关问题,主要是考虑一下的关键字:锁对象.条件对象 -> synchronized wait() notify(). 1.关于锁对象与条件对象: 所对象的定义在ja ...

  2. 基于Apache搭建HTTP HTTPS

    参考资料 <openssl攻略>--第一章 <Apache服务器配置与使用工作笔记>-- 第六章 第十四章 https://juejin.im/post/5a31faf2518 ...

  3. phpstudy开启PHPSocket扩展(windows系统)

    PHP开启Socket扩展 一.windows系统(本地电脑) 1.打开phpstudy,设置——>配置文件——>打开php.ini(我安装的是PhpStudy v8.0,其他版本请自己找 ...

  4. Codeforces 1166E(思维)

    题面 有一个长度为n的序列a,有m次操作.每一次操作一个人选a的一个子集x,另一个人会选x的补集y.且x集合中的数的最小公倍数比y集合中的数的最小公倍数大.现在给出所有x,判断是否有一个序列a满足条件 ...

  5. k3 cloud中列表字段汇总类型中设置了汇总以后没有显示出汇总值

    处理方法,需要bos中的分组列信息中设置求和: 选择对应字段将其从左侧添加到右侧 方法二: 直接针对[价税合计]字段的列表汇总类型进行设置,步骤如下图所示

  6. XMPP即时通讯协议使用(一)——Openfire安装

    Openfire服务器安装 下载地址:https://www.igniterealtime.org/downloads/index.jsp,根据你的操作系统,选择对应的下载版本.本文选择的是openf ...

  7. css浮动、定位到底什么鬼?

    css操作元素位置有以下几种方式:float.position.top等. I float part 1.浮动首先会先将元素在正常文档流中删除,父容器无法获取元素高度,但是该元素依然影响布局. 2.任 ...

  8. iconfont图标symbol引用方式,有的图标不能通过设置color样式来修改颜色的解决办法

    现象:iconfont安装后的图标,是通过symbol引用方式,有的图标不能通过color修改颜色的解决办法,有的又可以. <svg class="icon" aria-hi ...

  9. 【LeetCode】几何学 geometry(共2题)

    [587]Erect the Fence [892]Surface Area of 3D Shapes

  10. Sass-变量计算

    在 Sass 中除了可以使用数值进行运算之外,还可以使用变量进行计算,其实在前面章节的示例中也或多或少的向大家展示了.在 Sass 中使用变量进行计算,这使得 Sass 的数学运算功能变得更加实用.一 ...