POJ-3349 Snowflake Snow Snowflakes 水题

题意:有n个雪花,每片雪花6个长度,当且仅当两片雪花对应位置的数字相同时为相同的雪花,雪花可以顺时针旋转、翻折。

比如1 2 3 4 5 6 和 1 6 5 4 3 2 或者 2 3 4 5 6 1是相同的雪花。

思路:把每片雪花的顺时针序和逆时针序共12个取字典序最小的存下来。然后只要sort一下n个雪花序列然后找有没有相同的即可。

/* 
time:2532ms 
memory:3000kB
*/
#include<iostream>
#include<string>
#include<cstring>
#include<stdlib.h>
#include<cstdio>
#include<stdio.h>
#include<set>
#include<map>
#include<deque>
#include<stack>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<list>
#include<bitset>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxint = ~0U >> 1;
const int maxn = 100010;
int tmp[6];
struct snow
{
public:
	int a[6];
	snow(int* x)
	{
		snow tmp;bool ok = false;
		memset(a, 0x3f, sizeof a);
		for (int i = 0;i < 6;i++)
		{
			for (int j = 0;j < 6;j++)//顺时针序
				tmp.a[j] = x[(i + j) % 6];
			if (tmp < *this)
				*this = tmp;
			for (int j = 0;j < 6;j++)//逆时针序
				tmp.a[j] = x[(i - j + 6) % 6];
			if (tmp < *this)
				*this = tmp;
		}
	}
	snow() {}
	friend bool operator <(const snow& a,const snow& b)//按字典序排序
	{
		int t = 0;
		while (a.a[t] == b.a[t] && t < 5)t++;
		return a.a[t] < b.a[t];
	}
	bool operator ==(const snow& b)
	{
		for (int i = 0;i < 6;i++)
			if (a[i] != b.a[i])return false;
		return true;
	}
}s[maxn];
int main()
{
	int n;
	bool ok = false;
	scanf("%d", &n);
	for (int i = 0;i < n;i++)
	{
		for (int j = 0;j < 6;j++)
			scanf("%d", tmp + j);
		s[i] = snow(tmp);
	}
	sort(s, s + n);
	for (int i = 0;i < n - 1;i++)
	{
		if (s[i] == s[i + 1])
		{
			ok = true;
			break;
		}
	}
	if(ok)
		printf("Twin snowflakes found.\n");
	else
		printf("No two snowflakes are alike.\n");
	return 0;
}

/****************************************更新线*********************************************/

更新:使用了快速读入后时间压缩至719ms

#include<iostream>
#include<string>
#include<cstring>
#include<stdlib.h>
#include<cstdio>
#include<stdio.h>
#include<set>
#include<map>
#include<deque>
#include<stack>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<list>
#include<bitset>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxint = ~0U >> 1;
char * cp = (char *)malloc(20000000);
inline void in(int &x) {
while (*cp<'0' || *cp>'9')++cp;
for (x = 0;*cp >= '0'&&*cp <= '9';)x = x * 10 + (*cp++^'0');
}
const int maxn = 100010;
int tmp[6];
struct snow
{
public:
	int a[6];
	snow(int* x)
	{
		snow tmp;bool ok = false;
		memset(a, 0x3f, sizeof a);
		for (int i = 0;i < 6;i++)
		{
			for (int j = 0;j < 6;j++)
				tmp.a[j] = x[(i + j) % 6];
			if (tmp < *this)
				*this = tmp;
			for (int j = 0;j < 6;j++)
				tmp.a[j] = x[(i - j + 6) % 6];
			if (tmp < *this)
				*this = tmp;
		}
	}
	snow() {}
	friend bool operator <(const snow& a,const snow& b)
	{
		int t = 0;
		while (a.a[t] == b.a[t] && t < 5)t++;
		return a.a[t] < b.a[t];
	}
	bool operator ==(const snow& b)
	{
		for (int i = 0;i < 6;i++)
			if (a[i] != b.a[i])return false;
		return true;
	}
}s[maxn];
int main()
{
	int n;
	fread(cp, 1, 20000000, stdin);
	bool ok = false;
	in(n);
	for (int i = 0;i < n;i++)
	{
		for (int j = 0;j < 6;j++)
			in(tmp[j]);
		s[i] = snow(tmp);
	}
	sort(s, s + n);
	for (int i = 0;i < n - 1;i++)
	{
		if (s[i] == s[i + 1])
		{
			ok = true;
			break;
		}
	}
	if(ok)
		printf("Twin snowflakes found.\n");
	else
		printf("No two snowflakes are alike.\n");
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值