首页 > 学院 > 开发设计 > 正文

相同的雪花

2019-11-14 10:26:38
字体:
来源:转载
供稿:网友

相同的雪花

时间限制:1000 ms  |  内存限制:65535 KB难度:4描述 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 the input will contain a single interger T(0<T<10),the number of the test cases.The first line of every test case 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.输出For each test case,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.样例输入
121 2 3 4 5 64 3 2 1 6 5样例输出
Twin snowflakes found.

解题报告:Hash算法的简单应用。对arms和Hash下就行了。

code:

#include<iostream>#include<stdio.h>#include<queue>#include<vector>#include<stack>#include<cstring>#include<algorithm>using namespace std;typedef long long ll;const int maxn=100005;const int gcd=100000;int snow[maxn][6];  //存雪花arms值vector<int> v[maxn]; //存相同arms和的雪花bool deal(int a,int b){  //比较两个雪花是否相同    for(int i=0;i<6;i++){        if(           (snow[a][0]==snow[b][i] &&      //顺时针比较           snow[a][1]==snow[b][(i+1)%6] &&           snow[a][2]==snow[b][(i+2)%6] &&           snow[a][3]==snow[b][(i+3)%6] &&           snow[a][4]==snow[b][(i+4)%6] &&           snow[a][5]==snow[b][(i+5)%6])            ||           (snow[a][0]==snow[b][i] &&      //逆时针比较           snow[a][1]==snow[b][(i+5)%6] &&           snow[a][2]==snow[b][(i+4)%6] &&           snow[a][3]==snow[b][(i+3)%6] &&           snow[a][4]==snow[b][(i+2)%6] &&           snow[a][5]==snow[b][(i+1)%6])           )            return true;    }    return false;}int main(){  //  freopen("input.txt","r",stdin);    int t,n;    scanf("%d",&t);    while(t--){        memset(snow,0,sizeof(snow));        for(int i=0;i<maxn;i++){ //初始化            v[i].clear();        }        scanf("%d",&n);        for(int i=0;i<n;i++){            int sum=0;            for(int j=0;j<6;j++){                scanf("%d",&snow[i][j]);                sum+=snow[i][j];            }            sum=sum%gcd;            v[sum].push_back(i);        }        int flag=0;  //默认无相同雪花        for(int i=0;i<maxn;i++){            if(flag)                break;            if(v[i].size()>=2){                for(int j=0;j<v[i].size()-1;j++){                    for(int k=j+1;k<v[i].size();k++){                        if(deal(v[i][j],v[i][k])){                            flag=1;                            break;                        }                    }                }            }        }        if(!flag)            printf("No two snowflakes are alike./n");        else            printf("Twin snowflakes found./n");    }    return 0;}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表