POJ 3349 Snowflake Snow Snowflakes solving hash report

Snowflake Snow Snowflakes solving report

Problem-solving ideas: the first value of each of six angles add up snow, a large number of I take, I hash number as a subscript of an array, deposit into the snow. If two snowflakes exactly equal, then the subscript necessarily the same. List record with the same subscript snow, and then compared. Treatment process as long as the clockwise and counterclockwise direction on the line.
Here Insert Picture Description

#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;
}



Published 64 original articles · won praise 0 · Views 1454

Guess you like

Origin blog.csdn.net/weixin_45566331/article/details/104646692