Snowflake Snow Snowflakes

21 篇文章 0 订阅

Snowflake Snow Snowflakes ⁡ \operatorname{Snowflake\ Snow\ Snowflakes} Snowflake Snow Snowflakes

题目链接: POJ 3349 ⁡ \operatorname{POJ\ 3349} POJ 3349

题目大意

就是有很多的雪花片,每一个雪花片有六个瓣(众所周知)。我们告诉你每个雪花片的每个瓣的长度,问你有没有任意两个雪花片是相同的。

我们有没有雪花片相同,就看从任意一个雪花片的瓣开始数,按顺时针或者逆时针排出的一个序列。
然后如果两个雪花片组出来的序列有相同的,那这两个雪花片就是相同的。

题目

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.

输入

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.

输出

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.

样例输入

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

样例输出

Twin snowflakes found.

数据范围

0 < n ≤ 100000 0 < n ≤ 100000 0<n100000
每一个雪花片的瓣的长度都是大于等于 0 0 0 但小于 10000000 10000000 10000000 的整数。

思路

这道题是一道 hash 。

我们就可以累加雪花瓣的长度得出这个雪花的 hash 值(我这里取模用了 14997 14997 14997 这个素数)。
那如果 hash 值一样的,就直接暴力模拟顺时针或者逆时针的每一种摆法,看是否相同。

有一个要注意的就是 hash 值一样的不同雪花可能会有很多个,我们就要用数组来存下来。
(我是这样的, a [ i ] [ j ] [ k ] a[i][j][k] a[i][j][k] 表示 hash 值是 i i i ,是第 j j j 个 hash 值是这个的不同雪花,它的第 k k k 个瓣的长度)

代码

#include<cstdio>
#define mo 14997

using namespace std;

int n, a[14998][50][7], sum, num, hashnum[14998], tmp[7];
bool yes;

int main() {
	scanf("%d", &n);//读入
	for (int i = 1; i <= n; i++) {
		sum = 0;//初始化
		for (int j = 1; j <= 6; j++) {
			scanf("%d", &tmp[j]);//读入
			sum = (sum + tmp[j]) % mo;//hash
		}
		
		if (hashnum[sum]) {
			for (int tim = 1; tim <= hashnum[sum]; tim++) {//枚举每一个前面hash值与它相同的雪花
				for (int j = 1; j <= 6; j++) {//顺时针
					yes = 1;
					num = 1;
					for (int k = j; num <= 6; k = ((k == 6) ? 1 : (k + 1))) {
						if (a[sum][tim][num] != tmp[k]) {
							yes = 0;
							break;
						}
						num++;
					}
					if (yes) {//相同
						printf("Twin snowflakes found.");//输出
						return 0;
					}
				}
				
				for (int j = 1; j <= 6; j++) {//逆时针
					yes = 1;
					num = 1;
					for (int k = j; num <= 6; k = ((k == 1) ? 6 : (k - 1))) {
						if (a[sum][tim][num] != tmp[k]) {
							yes = 0;
							break;
						}
						num++;
					}
					if (yes) {//相同
						printf("Twin snowflakes found.");//输出
						return 0;
					}
				}
			}
		}
		
		hashnum[sum]++;//记录
		for (int j = 1; j <= 6; j++)
			a[sum][hashnum[sum]][j] = tmp[j];
	}
	
	printf("No two snowflakes are alike.");//输出
	
	return 0;
} 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
是的,你的基本步骤是正确的。以下是一个示例代码,展示如何使用C#来实现雪花漫天飞舞的场景: ```csharp using System; using System.Drawing; using System.Windows.Forms; struct SNOW { public Point position; public int size; public float speed; public float acceleration; } public partial class Form1 : Form { SNOW[] snowflakes = new SNOW[100]; Random random = new Random(); Timer timer = new Timer(); public Form1() { InitializeComponent(); // 初始化定时器 timer.Interval = 50; timer.Tick += new EventHandler(Timer_Tick); timer.Start(); } private void Form1_Paint(object sender, PaintEventArgs e) { // 绘制每一个雪花 foreach (SNOW snowflake in snowflakes) { e.Graphics.FillEllipse(Brushes.White, snowflake.position.X, snowflake.position.Y, snowflake.size, snowflake.size); } } private void Timer_Tick(object sender, EventArgs e) { // 更新每一个雪花的位置 for (int i = 0; i < snowflakes.Length; i++) { SNOW snowflake = snowflakes[i]; // 更新速度和位置 snowflake.speed += snowflake.acceleration; snowflake.position.Y += (int)snowflake.speed; // 如果雪花超出窗口,则重新生成 if (snowflake.position.Y > this.Height) { snowflake.position.X = random.Next(0, this.Width); snowflake.position.Y = -random.Next(50, 200); snowflake.size = random.Next(5, 15); snowflake.speed = random.Next(1, 4); snowflake.acceleration = (float)random.NextDouble() * 0.1f + 0.1f; } snowflakes[i] = snowflake; } // 重绘窗口 this.Invalidate(); } private void Form1_Load(object sender, EventArgs e) { // 初始化每一个雪花 for (int i = 0; i < snowflakes.Length; i++) { SNOW snowflake = new SNOW(); snowflake.position.X = random.Next(0, this.Width); snowflake.position.Y = -random.Next(50, 200); snowflake.size = random.Next(5, 15); snowflake.speed = random.Next(1, 4); snowflake.acceleration = (float)random.NextDouble() * 0.1f + 0.1f; snowflakes[i] = snowflake; } // 将背景设置为黑色 this.BackColor = Color.Black; } } ``` 通过上述代码,你可以在C#中实现雪花漫天飞舞的场景。希望这可以帮到你。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值