POJ 3349スノーフレーク雪スノーフレーク解決ハッシュレポート

スノーフレーク雪スノーフレーク解決レポート

問題解決のアイデア:6つの角度のそれぞれの最初の値Iが多数、配列、雪への預金の添字としてIハッシュ番号を取る、雪を追加します。2つの雪が完全に等しい場合には、添字必ずしも同じ。その後、リスト同じ添字雪を持つレコード、および比較。処理プロセス長いライン上の時計回りと反時計方向と同じくらい。
ここに画像を挿入説明

#include<iostream>
#include<math.h>
#include<iomanip>
#include<algorithm>
#include<iostream>
#include<math.h>
#include<iomanip>
#include<algorithm>
#include<queue>
#include<cstring>
#include<string>
#include<map>
#include<stack>
#include<stdio.h>
#include<cstdio>
#include<stdlib.h>
#include<fstream>
#include<iomanip>
#pragma warning(disable:4996)
#define INF 0x3f3f3f3f
#define ll long long
#define PI acos(-1.0)
const int N = 1000010;
const int maxn = 1e9;
using namespace std;
struct snow {
	int a[6];
	int pos;//记录在哈希表中位置,即key
	snow* next;
};
snow* ha[1000000];//用来放首指针地址
bool cmp(snow* x, snow* y)//y新x旧
{
	int i, j;
	for (i = 0; i < 6; i++)//顺时针
	{
		for (int j = 0; j < 6; j++)
		{
			if (y->a[j] != x->a[(i + j) % 6])
				break;
			if (5 == j)
				return true;
		}
	}
	for (i = 0; i < 6; i++)//逆时针
	{
		for (int j = 0; j < 6; j++)
		{
			if (y->a[j] != x->a[(20 - i - j) % 6])//20可以换任意数,保证数组下标>-1就行
				break;
			if (5 == j)
				return true;
		}
	}
	return false;
}
int main()
{
	int n;
	scanf("%d", &n);
	bool flag = false;
	while (n--)
	{
		snow* head;
		head = (snow*)malloc(sizeof(snow));
		int sum = 0;
		for (int i = 0; i < 6; i++)
		{
			scanf("%d", &head->a[i]);
			sum += head->a[i] % 999983;//100万内最大素数是999983
		}
		if (flag)
			continue;
		sum %= 999983;
		head->pos = sum;
		head->next = NULL;
		if (NULL==ha[head->pos])
		{
			ha[head->pos] = head;//存入首指针地址
		}
		else
		{
			snow* tem = ha[head->pos];
			while (tem!= NULL)
			{
				if (cmp(tem, head))
				{
					flag = true;
					break;
				}
				else
				{
					tem = tem->next;
				}
			}
			tem = head;
		}
	}
	if (flag)
		printf("Twin snowflakes found.\n");
	else
		printf("No two snowflakes are alike.\n");
	return 0;
}



公開された64元の記事 ウォンの賞賛0 ビュー1454

おすすめ

転載: blog.csdn.net/weixin_45566331/article/details/104646692