-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy path10212DNim.cpp
More file actions
91 lines (88 loc) · 2.27 KB
/
10212DNim.cpp
File metadata and controls
91 lines (88 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include<iostream>
#include<stdio.h>
#include<vector>
#include<utility>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef unsigned long long ULL;
typedef pair<int,int> pii;
vector<pii> p;
int board[100][100]; // W,H
int W,H;
inline ULL sqr(int x){
return x*x;
}
ULL hash1(){
ULL h = 0;
for(int i=0;i<p.size();i++)
for(int j=i+1;j<p.size();j++){
h += sqr(p[i].first-p[j].first) + sqr(p[i].second-p[j].second);
}
return h;
}
void dfs(int i,int j){
for(int dx=-1;dx<=1;dx++)
for(int dy=-1;dy<=1;dy++)
if((dx==0)^(dy==0)){
if(i+dx<0 || i+dx>=W || j+dy<0 || j+dy>=H) continue;
if(board[i+dx][j+dy]){
board[i+dx][j+dy] = 0;
p.push_back(make_pair(i+dx,j+dy));
dfs(i+dx,j+dy);
}
}
}
int main()
{
int t,x,y;
scanf("%d",&t);
while(t--){
int k;
scanf("%d%d",&W,&H);
scanf("%d",&k);
memset(board,0,sizeof board);
for(int i=0;i<k;i++){
scanf("%d%d",&x,&y);
board[x][y] = 1;
}
vector<ULL> a;
for(int i=0;i<W;i++)
for(int j=0;j<H;j++)
if(board[i][j]){
board[i][j] = 0;
p.clear();
p.push_back(make_pair(i,j));
dfs(i,j);
a.push_back(hash1());
}
memset(board,0,sizeof board);
for(int i=0;i<k;i++){
scanf("%d%d",&x,&y);
board[x][y] = 1;
}
vector<ULL> b;
for(int i=0;i<W;i++)
for(int j=0;j<H;j++)
if(board[i][j]){
board[i][j] = 0;
p.clear();
p.push_back(make_pair(i,j));
dfs(i,j);
b.push_back(hash1());
}
if(a.size()!=b.size()){
printf("NO\n");
continue;
}
bool ok = true;
sort(a.begin(),a.end());
sort(b.begin(),b.end());
for(int i=0;i<a.size();i++)
if(a[i]!=b[i]) ok = false;
if(ok) printf("YES\n");
else printf("NO\n");
}
return 0;
}