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. [LeetCode] 137. Single Number II (位操作)

    传送门 Description Given an array of integers, every element appears three times except for one, which ...

  2. UI自动化处理文件上传

    UI自动化处理文件上传 import win32guiimport win32con def set_uploader(self, file_path): sleep(2) self.file_pat ...

  3. luoguP2123 皇后游戏(贪心)

    luoguP2123 皇后游戏(贪心) 题目 洛谷题目chuanso 题解 有一篇好题解,我就懒得推式子了,毕竟打到电脑上还是很难的 牛逼题解传送门 code #include<iostream ...

  4. Ajax爬取百度图片

    目标网址 分析网址:http://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2& ...

  5. 剑指offer学习--初级c++面试题

    定义一个空的类型,里面没有任何成员函数和成员变量,对该类型求sizeof,得到的结果是多少? 答案是1.空类型中的实例中不包含任何信息,本来求sizeof应该是0,但是当我们声明该类型的实例的时候,他 ...

  6. elasticsearch 深入 —— normalizer

    keyword字段的normalizer属性类似于分析器,只是它保证分析链生成单个token. 在索引关键字之前,以及在通过诸如match查询之类的查询解析器或者通过诸如term查询之类的术语级查询搜 ...

  7. 2019-9-2-win10-uwp-兴趣线

    title author date CreateTime categories win10 uwp 兴趣线 lindexi 2019-09-02 12:57:38 +0800 2018-2-13 17 ...

  8. 免费资源(CDN,顶级域名)汇集

    CloudFlare:免费CDN,需要将域名指向到cloudflare服务器.付费的可以使用二级域名 https://www.cloudflare.com/ Freenom:freenom会提供免费提 ...

  9. linux ---JDK的安装与配置--两种方式

    linux下的JDK的安装与配置:JDK的安装有两种方式: tar包和rpm安装1.tar包安装: 下载地址:http://www.oracle.com/technetwork/java/javase ...

  10. HTML基础用 表格做报表

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...