poj3349(Snowflake Snow Snowflakes)

题目地址:Snowflake Snow Snowflakes

题目大意:

给你N个雪花,每个雪花由6个长度构成,让你判断N哥雪花中是否有两个或两个以上的雪花是相似的,相似的规则是:有两个雪花a、b。b在任意位置顺时针或者逆时针转和a的大小顺序相同即为相似的雪花。

解题思路;

一般的方法如果时间复杂度是O(n^2)的都会超时,所以要尽量让N小,所有用个简单哈希。将6个数的和对N取余存到一个vector。然后再在vector里找看是否有相似的雪花。

代码:

  1 #include <algorithm>
  2 #include <iostream>
  3 #include <sstream>
  4 #include <cstdlib>
  5 #include <cstring>
  6 #include <cstdio>
  7 #include <string>
  8 #include <bitset>
  9 #include <vector>
 10 #include <queue>
 11 #include <stack>
 12 #include <cmath>
 13 #include <list>
 14 //#include <map>
 15 #include <set>
 16 using namespace std;
 17 /***************************************/
 18 #define ll long long
 19 #define int64 __int64
 20 /***************************************/
 21 const int INF = 0x7f7f7f7f;
 22 const double eps = 1e-8;
 23 const double PIE=acos(-1.0);
 24 const int d1x[]= {0,-1,0,1};
 25 const int d1y[]= {-1,0,1,0};
 26 const int d2x[]= {0,-1,0,1};
 27 const int d2y[]= {1,0,-1,0};
 28 const int fx[]= {-1,-1,-1,0,0,1,1,1};
 29 const int fy[]= {-1,0,1,-1,1,-1,0,1};
 30 /*vector <int>map[N];map[a].push_back(b);int len=map[v].size();*/
 31 /***************************************/
 32 void openfile()
 33 {
 34     freopen("data.in","rb",stdin);
 35     freopen("data.out","wb",stdout);
 36 }
 37 /**********************华丽丽的分割线,以上为模板部分*****************/
 38 const int N=999;
 39 int n;
 40 typedef struct
 41 {
 42     int a[6];
 43     long long sum;
 44 } Node;
 45 Node node;
 46 vector <Node>vc[N];
 47 int cmp2(int a1[],int a2[])
 48 {
 49     int i,j;
 50     int cnt1=0,cnt2=0;
 51     for(i=0; i<6; i++)
 52     {
 53         if (a1[0]==a2[i])
 54         {
 55             for(cnt1=0,j=1; j<6; j++)
 56             {
 57                 int v=i+j;
 58                 if (v>=6)
 59                     v-=6;
 60                 if (a1[j]==a2[v])
 61                     cnt1++;
 62             }
 63             if (cnt1==5)
 64                 return 1;
 65             for(cnt2=0,j=1; j<6; j++)
 66             {
 67                 int v=i-j;
 68                 if (v<0)
 69                     v+=6;
 70                 if (a1[j]==a2[v])
 71                     cnt2++;
 72             }
 73             if (cnt2==5)
 74                 return 1;
 75         }
 76     }
 77     return 0;
 78 }
 79 int cmp1()
 80 {
 81     int i,j,k;
 82     for(i=0; i<N; i++)
 83     {
 84         for(j=0; j<vc[i].size(); j++)
 85             for(k=j+1; k<vc[i].size(); k++)
 86             {
 87                 if (vc[i][j].sum==vc[i][k].sum&&cmp2(vc[i][j].a,vc[i][k].a))
 88                     return 1;
 89             }
 90     }
 91     return 0;
 92 }
 93 int main()
 94 {
 95     while(scanf("%d",&n)!=EOF)
 96     {
 97         int i,j;
 98         for(i=0; i<n; i++)
 99         {
100             node.sum=0;
101             for(j=0; j<6; j++)
102             {
103                 scanf("%d",&node.a[j]);
104                 node.sum+=node.a[j];
105             }
106             vc[node.sum%N].push_back(node);
107         }
108         if (cmp1())
109             printf("Twin snowflakes found.\n");
110         else
111             printf("No two snowflakes are alike.\n");
112         for (i=0;i<N;i++)
113             vc[i].clear();
114     }
115     return 0;
116 }
117 /*
118 2
119 1 2 3 4 5 6
120 2 3 4 5 6 7
121 2
122 1 2 3 1 3 2
123 1 3 2 1 2 3
124 .*/

poj3349(Snowflake Snow Snowflakes)

时间: 2024-10-22 23:12:15

poj3349(Snowflake Snow Snowflakes)的相关文章

POJ3349 Language: Snowflake Snow Snowflakes

POJ3349 Language: Snowflake Snow Snowflakes 题目:传送门 题解: 链表+hash的一道水题 填个坑补个漏... 代码: 1 #include<cstdio> 2 #include<cstring> 3 #include<cstdlib> 4 #include<cmath> 5 #include<algorithm> 6 using namespace std; 7 typedef long long L

poj 3349:Snowflake Snow Snowflakes(哈希查找,求和取余法+拉链法)

Snowflake Snow Snowflakes Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 30529   Accepted: 8033 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. Y

[ACM] POJ 3349 Snowflake Snow Snowflakes(哈希查找,链式解决冲突)

Snowflake Snow Snowflakes Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 30512   Accepted: 8024 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. Y

POJ 3349 Snowflake Snow Snowflakes (哈希表)

题意:每片雪花有六瓣,给出n片雪花,六瓣花瓣的长度按顺时针或逆时针给出,判断其中有没有相同的雪花(六瓣花瓣的长度相同) 思路:如果直接遍历会超时,我试过.这里要用哈希表,哈希表的关键码key用六瓣花瓣的长度的和取余一个数得到,表中为雪花的存储位置address(即在snowflakes数组中的位置) 代码: #include<iostream> #include<vector> using namespace std; const int maxn=100000+100;//雪花最

POJ 3349 Snowflake Snow Snowflakes(哈希表)(转)

题意:判断有没有两朵相同的雪花.每朵雪花有六瓣,比较花瓣长度的方法看是否是一样的,如果对应的arms有相同的长度说明是一样的.给出n朵,只要有两朵是一样的就输出有Twin snowflakes found.,如果任何两个都是不一样的输出No two snowflakes are alike.n=100,000. 思路:最 简单的就是枚举每两片雪花,判断他们是否相同.时间复杂度为O(n*n),显然效果不理想.有没有更好的算法呢?hash:每读进一片雪花,将雪花 hash,判断hash表里是否有相同

POJ 3349:Snowflake Snow Snowflakes(数的Hash)

http://poj.org/problem?id=3349 Snowflake Snow Snowflakes Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 37609   Accepted: 9878 Description You may have heard that no two snowflakes are alike. Your task is to write a program to determine

POJ3349 Snowflake Snow Snowflakes (hash

Snowflake Snow Snowflakes Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 48624   Accepted: 12697 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.

[poj3349]Snowflake Snow Snowflakes(hash)

Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 37615 Accepted: 9882 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

hash应用以及vector的使用简介:POJ 3349 Snowflake Snow Snowflakes

今天学的hash.说实话还没怎么搞懂,明天有时间把知识点总结写了,今天就小小的写个结题报告吧! 题意: 在n (n<100000)个雪花中判断是否存在两片完全相同的雪花,每片雪花有6个角,每个角的长度限制为1000000 两片雪花相等的条件: 雪花6个角的长度按顺序相等(这个顺序即可以是顺时针的也可以是逆时针的) 解题思路: hash:连加求余法 求key 值,链地址法解决冲突,连加求余法 求key 值挺简单,关于链地址法解决冲突可以通过c++中,vector容器可以较为方便的实现. 下面先介绍