Snow POJ3349

Snow POJ3349

Thinking

Casual working in this: POJ3349
data is relatively large, it first with Hash discrete, another one by comparison. Two snowflakes violent than I was, but also with the smallest representation optimization method. The title data and more attention cin timeout, use scanf.

ac Code

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#define MAX 100010
#define P 99991
using namespace std;
int snow[MAX][6] = { 0 };
int head[MAX] = { 0 };
int nextp[MAX] = { 0 };
int tot = 1;
int Hash(int a[]) {
	int add = 0, mul = 1;
	for (int i = 0; i < 6; i++) {
		add = (add + a[i]) % P;
		mul = long long(mul) * long long(a[i] % P) % P;
	}
	return (add + mul) % P;
}
bool equal(int a[], int snow[]) {
	for (int i = 0; i < 6; i++) {
		bool same = 1;
		for (int j = 0; j < 6; j++) {
			if (a[j] != snow[(i + j) % 6]) {
				same = 0;
				break;
			}
		}
		if (same)
			return 1;
		same = 1;
		for (int j = 0; j < 6; j++) {
			if (a[j] != snow[(6 + i - j) % 6]) {
				same = 0;
				break;
			}
		}
		if (same)
			return 1;
	}
	return 0;
}
int insert(int a[]) {
	int val = Hash(a);
	for (int i = head[val]; i != 0; i = nextp[i]) {
		if (equal(a, snow[i]))
			return 1;
	}
	memcpy(snow[tot], a, 6 * sizeof(int));
	nextp[tot] = head[val];
	head[val] = tot;
	tot++;
	return 0;
}
int main() {
	int n;
	cin >> n;
	int a[6];
	for (int i = 1; i <= n; i++) {
		scanf_s("%d%d%d%d%d%d", a, a + 1, a + 2, a + 3, a + 4, a + 5);
		if (insert(a)) {
			puts("Twin snowflakes found.");
			return 0;
		}
	}
	puts("No two snowflakes are alike.");
	return 0;
}
Published 14 original articles · won praise 0 · Views 196

Guess you like

Origin blog.csdn.net/qq_45616764/article/details/104216369