poj1021

//============================================================================
// Name        : 1021.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <stdio.h>
#include <cstring>
#include <vector>
#include <algorithm>
#define W 100
#define H 100

using namespace std;

typedef struct node {
	int direction;
	int size;
	struct node* connect[4];
} Node;

typedef struct sum {
	int size;
	int distance;
	int connect_size[4];
} Sum;

int com(const Sum& a, const Sum& b) {
	if (a.size != b.size)
		return a.size < b.size;
	if (a.distance != b.distance)
		return a.distance < b.distance;
	for (int i = 0; i < a.size; i++) {
		if (a.connect_size[i] != b.connect_size[i])
			return a.connect_size[i] < b.connect_size[i];
	}
	return 0;
}
bool equal(const Sum& a, const Sum& b) {
	if (a.size != b.size)
		return false;
	if (a.distance != b.distance)
		return false;
	for (int i = 0; i < a.size; i++) {
		if (a.connect_size[i] != b.connect_size[i])
			return false;
	}
	return true;
}
int w, h, n;
Node g1[W][H];
Node g2[W][H];
vector<Node*> v1;
vector<Node*> v2;
vector<Sum> s1;
vector<Sum> s2;

void connect(Node& a, Node& b, int direction) {
	a.connect[a.size++] = &b;
	b.connect[b.size++] = &a;
	a.direction += direction;
	b.direction += direction;
}

Sum toSum(Node * n) {
	Sum s;
	s.size = n->size;

	if (n->direction & 10)
		s.distance = 2;
	else
		s.distance = 1;
	for (int i = 0; i < n->size; i++) {
		s.connect_size[i] = n->connect[i]->size;
	}
	sort(s.connect_size, s.connect_size + s.size);
	return s;
}

void outputNode(Node* n) {
	printf("number=%d(%d):", n->direction, n->size);
	for (int i = 0; i < n->size; i++) {
		printf(" %d", n->connect[i]->direction);
	}
	puts("");
}

void outSum(Sum& s) {
	printf("size=%d distance=%d:", s.size, s.distance);
	for (int i = 0; i < s.size; i++) {
		printf(" %d", s.connect_size[i]);
	}
	puts("");
}

int main() {
	int t;
	scanf("%d", &t);
	while (t--) {
		scanf("%d %d %d", &w, &h, &n);
		memset(g1, 0, sizeof(g1));
		v1.clear();
		for (int i = 1; i <= n; i++) {
			int x, y;
			scanf("%d %d", &x, &y);
			g1[x][y].direction = 16;
			v1.push_back(&g1[x][y]);
			if (x > 0 && g1[x - 1][y].direction > 0) {
				connect(g1[x][y], g1[x - 1][y], 1);
			}
			if (y > 0 && g1[x][y - 1].direction > 0) {
				connect(g1[x][y], g1[x][y - 1], 4);
			}
			if (x < w - 1 && g1[x + 1][y].direction > 0) {
				connect(g1[x][y], g1[x + 1][y], 1);
			}
			if (y < h - 1 && g1[x][y + 1].direction > 0) {
				connect(g1[x][y], g1[x][y + 1], 4);
			}
		}

		memset(g2, 0, sizeof(g2));
		v2.clear();
		for (int i = 1; i <= n; i++) {
			int x, y;
			scanf("%d %d", &x, &y);
			g2[x][y].direction = 16;
			v2.push_back(&g2[x][y]);
			if (x > 0 && g2[x - 1][y].direction > 0) {
				connect(g2[x][y], g2[x - 1][y], 1);
			}
			if (y > 0 && g2[x][y - 1].direction > 0) {
				connect(g2[x][y], g2[x][y - 1], 4);
			}
			if (x < w - 1 && g2[x + 1][y].direction > 0) {
				connect(g2[x][y], g2[x + 1][y], 1);
			}
			if (y < h - 1 && g2[x][y + 1].direction > 0) {
				connect(g2[x][y], g2[x][y + 1], 4);
			}
		}

		s1.clear();
		s2.clear();
		for (int i = 0; i < n; i++) {
			Sum s = toSum(v1[i]);
			s1.push_back(s);
			s = toSum(v2[i]);
			s2.push_back(s);
		}

//		puts("g1");
//		for (int i = 0; i < n; i++) {
//			outputNode(v1[i]);
//			outSum(s1[i]);
//		}
//
//		puts("\ng2");
//		for (int i = 0; i < n; i++) {
//			outputNode(v2[i]);
//			outSum(s2[i]);
//		}
//		puts("");

		sort(s1.begin(), s1.end(), com);
		sort(s2.begin(), s2.end(), com);

		bool re = true;
		for (int i = 0; i < n && re; i++) {
//			outSum(s1[i]);
//			outSum(s2[i]);
			if (!equal(s1[i], s2[i])) {
				re = false;
			}
		}
		if (re) {
			puts("YES");
		} else {
			puts("NO");
		}
	}

	return 0;
}

猜你喜欢

转载自blog.csdn.net/swwlqw/article/details/13999933