POJ3349-Snowflake Snow Snowflakes

题目链接:点击打开链接

SnowflakeSnowSnowflakes

Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 48324   Accepted: 12621

Description

You may have heard that no two snowflakes are alike. Your task is to write a program to determine whether this is really true. Your program will read information about a collection of snowflakes, and search for a pair that may be identical. Each snowflake has six arms. For each snowflake, your program will be provided with a measurement of the length of each of the six arms. Any pair of snowflakes which have the same lengths of corresponding arms should be flagged by your program as possibly identical.

Input

The first line of input will contain a single integer n, 0 < n ≤ 100000, the number of snowflakes to follow. This will be followed by n lines, each describing a snowflake. Each snowflake will be described by a line containing six integers (each integer is at least 0 and less than 10000000), the lengths of the arms of the snow ake. The lengths of the arms will be given in order around the snowflake (either clockwise or counterclockwise), but they may begin with any of the six arms. For example, the same snowflake could be described as 1 2 3 4 5 6 or 4 3 2 1 6 5.

Output

If all of the snowflakes are distinct, your program should print the message:
No two snowflakes are alike.
If there is a pair of possibly identical snow akes, your program should print the message:
Twin snowflakes found.

Sample Input

2
1 2 3 4 5 6
4 3 2 1 6 5

Sample Output

Twin snowflakes found.

题目大意:给出n个雪花的六个花瓣的长度,每一都是顺时针或者逆时针从任何位置给出,然后判断是否有两个一样的雪花。

思路:本来是想查找每一个下面的数字,是不是上面的都有,然后再判断两边的数字是不是一样(相反位置也行)。但是貌似是行不通的,后来看了题解。需要先哈希所有的长度,长度一样了再去分顺时针和逆时针去匹配六个长度。

AC代码:

#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
const int MAX = 100010;
const int mod = 99991;

vector<int> hsh[mod];//如果mod不加 const 会报错不能识别hsh 
int arm[MAX][6];
int n;

bool issame(int a, int b) {//逆时针or顺时针匹配 (我最开始是找一个数两边的值是否相等) 
	for(int i = 0; i < 6; i++) {
		if((arm[a][0] == arm[b][i] && arm[a][1] == arm[b][(i+1)%6] &&
		    arm[a][2] == arm[b][(i+2)%6] && arm[a][3] == arm[b][(i+3)%6] &&
			arm[a][4] == arm[b][(i+4)%6] && arm[a][5] == arm[b][(i+5)%6])
			||
			(arm[a][0] == arm[b][i] && arm[a][1] == arm[b][(i+5)%6] &&
			arm[a][2] == arm[b][(i+4)%6] && arm[a][3] == arm[b][(i+3)%6] &&
			arm[a][4] == arm[b][(i+2)%6] && arm[a][5] == arm[b][(i+1)%6]))
			return true;
	}
	return false;
}

int main() {
	scanf("%d", &n);
	long long sum, key;
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < 6; j++) {
			scanf("%d", &arm[i][j]);
		}
	}
	for(int i = 0; i < n; i++) {
		sum = 0;
		for(int j = 0; j < 6; j++) {
			sum += arm[i][j];
		}
		key = sum % mod; 
		for(int k = 0; k < hsh[key].size(); k++) {//如果是空的,就进不去 
			if(issame(i, hsh[key][k])) {//不是空的,就去匹配顺序 
				printf("Twin snowflakes found.\n");
				return 0;
			}
		}
		hsh[key].push_back(i);//第i个放到该key里 (映射关系) 
	}
	printf("No two snowflakes are alike.\n");
} 

猜你喜欢

转载自blog.csdn.net/qq_40932661/article/details/81054264
今日推荐