poj 1021 2D-Nim 模拟

题意:

给两个平面,每个平面上有一些点,相邻的点可构成点集,为两个平面内的点集是够都对应相似。两个点集相似是指经过对称或旋转或平移后相等。

分析:

直接模拟判断。

代码:

//poj 1021
//sep9
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int w,h,n;
int g[128][128];
int vis[128][128];
int dirx[4]={0,0,-1,1};
int diry[4]={-1,1,0,0};
struct P
{
	int x,y;
};
struct AREA
{
	vector<P> pnts;
}area;
void dfs(int x,int y)
{
	P p0;
	p0.x=x,p0.y=y;
	vis[x][y]=1;
	area.pnts.push_back(p0);
	for(int k=0;k<4;++k){
		int nx=x+dirx[k];
		int ny=y+diry[k];
		if(nx>=0&&nx<h&&ny>=0&&ny<w&&vis[nx][ny]==0&&g[nx][ny]==1)
			dfs(nx,ny);
	}
}
struct BOARD
{
	vector<AREA> areas;
	void ini(){
		memset(g,0,sizeof(g));
		memset(vis,0,sizeof(vis));
		for(int i=0;i<n;++i){
			int r,c;
			scanf("%d%d",&c,&r);
			g[r][c]=1;
		}
		for(int i=0;i<h;++i)
			for(int j=0;j<w;++j){
				if(vis[i][j]==0&&g[i][j]==1){
					area.pnts.clear();
					dfs(i,j);
					areas.push_back(area);
				}
			}
	}
};
BOARD a,b;

bool cmp(P u,P v)
{
	if(u.x!=v.x)
		return u.x<v.x;
	return u.y<v.y;
}

bool judge(AREA p,AREA q)
{
	sort(p.pnts.begin(),p.pnts.end(),cmp);
	sort(q.pnts.begin(),q.pnts.end(),cmp);
	int dx=q.pnts[0].x-p.pnts[0].x;
	int dy=q.pnts[0].y-p.pnts[0].y;
	for(int i=p.pnts.size()-1;i>=0;--i)
		if(p.pnts[i].x+dx!=q.pnts[i].x||p.pnts[i].y+dy!=q.pnts[i].y)
			return false;
	return true;
}

bool compare(AREA p,AREA q)
{
	AREA tmp;
	P tmp_p;
	for(int i=0;i<q.pnts.size();++i)
		tmp.pnts.push_back(q.pnts[i]);
	if(judge(p,tmp))
		return true;

	tmp.pnts.clear();
	for(int i=0;i<q.pnts.size();++i){
		tmp_p.x=-q.pnts[i].x;
		tmp_p.y=-q.pnts[i].y;
		tmp.pnts.push_back(tmp_p);
	}
	if(judge(p,tmp))
		return true;

	tmp.pnts.clear();
	for(int i=0;i<q.pnts.size();++i){
		tmp_p.x=q.pnts[i].x;
		tmp_p.y=-q.pnts[i].y;
		tmp.pnts.push_back(tmp_p);
	}
	if(judge(p,tmp))
		return true;

	tmp.pnts.clear();
	for(int i=0;i<q.pnts.size();++i){
		tmp_p.x=-q.pnts[i].x;
		tmp_p.y=q.pnts[i].y;
		tmp.pnts.push_back(tmp_p);
	}
	if(judge(p,tmp))
		return true;

	tmp.pnts.clear();
	for(int i=0;i<q.pnts.size();++i){
		tmp_p.x=q.pnts[i].y;
		tmp_p.y=q.pnts[i].x;
		tmp.pnts.push_back(tmp_p);
	}
	if(judge(p,tmp))
		return true;		

	tmp.pnts.clear();
	for(int i=0;i<q.pnts.size();++i){
		tmp_p.x=-q.pnts[i].y;
		tmp_p.y=-q.pnts[i].x;
		tmp.pnts.push_back(tmp_p);
	}
	if(judge(p,tmp))
		return true;

	tmp.pnts.clear();
	for(int i=0;i<q.pnts.size();++i){
		tmp_p.x=q.pnts[i].y;
		tmp_p.y=-q.pnts[i].x;
		tmp.pnts.push_back(tmp_p);
	}
	if(judge(p,tmp))
		return true;

	tmp.pnts.clear();
	for(int i=0;i<q.pnts.size();++i){
		tmp_p.x=-q.pnts[i].y;
		tmp_p.y=q.pnts[i].x;
		tmp.pnts.push_back(tmp_p);
	}
	if(judge(p,tmp))
		return true;

	return false;
}

bool find(AREA s,BOARD T)
{
	for(int i=0;i<T.areas.size();++i){
		if(s.pnts.size()!=T.areas[i].pnts.size())
			continue;
		if(compare(s,T.areas[i]))
			return true;
	}
	return false;
}

bool match()
{
	if(a.areas.size()!=b.areas.size())
		return false;
	for(int i=0;i<a.areas.size();++i)
		if(!find(a.areas[i],b))
			return false;
	for(int i=0;i<b.areas.size();++i)
		if(!find(b.areas[i],a))
			return false;
	return true;
}
int main()
{
	int cases;
	scanf("%d",&cases);
	while(cases--){
		scanf("%d%d%d",&w,&h,&n);
		a.areas.clear();
		b.areas.clear();
		a.ini();
		b.ini();
		if(match())
			puts("YES");
		else
			puts("NO");

	}
} 

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-07 22:27:11

poj 1021 2D-Nim 模拟的相关文章

poj 3399 Product(模拟)

# include <stdio.h> # include <string.h> # include <algorithm> using namespace std; int cmp(int x,int y) { return x>y; } int main() { int a[110],a1[110],a2[110],ans[110]; int n,k,k1,k2,i,k3; while(~scanf("%d%d",&n,&k

poj 2632 Crashing Robots, 模拟

点击打开链接 简单 模拟机器人的移动,发生碰撞时输出相应的信息. code #include <cstdio> #include <cstring> using namespace std; struct node{ int k; int s; } mtx[200][200]; struct node1{ int x, y; } rob[200]; int n, m; int dir[4][2]= {{0,1},{1,0},{0,-1},{-1,0}}; //N, E, S, W

POJ 1021 人品题

报告见代码.. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 7 const int MAX=105; 8 int dir[4][2]={1,0,-1,0,0,1,0,-1}; 9 int h,w,n; 10 int maze[MAX][MAX]; 11 int maze2[MAX][

【POJ】 Instant Complexity (模拟)

[POJ] Instant Complexity (模拟) Instant Complexity Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 1905   Accepted: 657 Description Analyzing the run-time complexity of algorithms is an important tool for designing efficient programs that

POJ 2632 Crashing Robots (模拟 坐标调整)(fflush导致RE)

题目链接:http://poj.org/problem?id=2632 先话说昨天顺利1Y之后,直到今天下午才再出题 TAT,真是刷题计划深似海,从此AC是路人- - 本来2632是道稍微恶心点的模拟,但毕竟是模拟,一般模拟都是只要示例过了基本就AC了,但这个题很特殊 我开始的时候一直跑不出测试示例,才发现题目中的插图和我构想的坐标系是不一样的(看来还不能轻易忽视掉插图啊) 这个题给出的坐标系是纵轴为y,横轴为x,y从下到上依次递增,x轴是从左到右递增 而我用的二维数组记录的地图,也就是说我的纵

POJ 3030. Nasty Hacks 模拟水题

Nasty Hacks Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13136   Accepted: 9077 Description You are the CEO of Nasty Hacks Inc., a company that creates small pieces of malicious software which teenagers may use to fool their friends.

HDU 1361 &amp; POJ 1068 Parencodings(模拟)

题目链接: HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1361 POJ:http://poj.org/problem?id=1068 Description Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in two different ways: q By an integer sequence P = p1 p2...pn where

POJ 3295 Tautology (栈模拟)

Tautology Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10096   Accepted: 3847 Description WFF 'N PROOF is a logic game played with dice. Each die has six faces representing some subset of the possible symbols K, A, N, C, E, p, q, r, s

POJ 1068 Parencodings (模拟),暑假第一题~

终于考完高线大,头也不那么疼了,也终于有空来刷题了~,先来一发水题试试水 题目链接:http://poj.org/problem?id=1068 简单的模拟,麻烦在理解题意,,英语好的请无视这句: [分析] S (((()()()))) P-sequence 4 5 6666 W-sequence 1 1 1456 如这个 S字符串, 题目给你一串数 4 5 6 6 6 6    对应P1 P2,,,Pn 意思是,第n个右括号')'左边有Pn 个左括号 ,,例如 456666 第1个右括号")&