http://poj.org/problem?id=3349
题意:给出n组数据,每组数据有六个数,这n组数据中若有两组数据不管是从某个数顺时针读还是逆时针读都相同,输出“Twin snowflakes found.”,否则,输出
"No two snowflakes are alike."
思路:将每组数据求和对大素数取余,将sum相同的放在同一个邻接表中,然后从邻接表中查找符合条件的数据。
1 #include <stdio.h> 2 #include <algorithm> 3 #include <string.h> 4 #include <vector> 5 using namespace std; 6 7 const int p=99983; 8 const int N=1000001; 9 int arr[N][7]; 10 struct node 11 { 12 int sum; 13 int id; 14 }; 15 vector<node>v[N];//将每组数据的sum相同放在v[sum]邻接表里; 16 17 int s_ee(int n,int m)//顺时针查找 18 { 19 int i,j; 20 for (i = 0; i < 6; i ++) 21 { 22 if(arr[n][i]==arr[m][0]) 23 { 24 for (j = 0; j < 6; j ++) 25 { 26 if (arr[n][(i+j)%6]!=arr[m][j]) 27 break; 28 } 29 if (j >= 6) 30 return 1; 31 } 32 } 33 return 0; 34 } 35 int ee_s(int n,int m)//逆时针查找 36 { 37 int i,j; 38 for (i = 0; i < 6; i ++) 39 { 40 if (arr[n][i]==arr[m][5]) 41 { 42 for (j = 0; j < 6; j ++) 43 { 44 if (arr[n][(i+j)%6]!=arr[m][5-j]) 45 break; 46 } 47 if (j >= 6) 48 return 1; 49 } 50 } 51 return 0; 52 } 53 int main() 54 { 55 int n,flag = 0 ,sum ; 56 scanf("%d",&n); 57 for (int i = 0; i <= N; i ++) 58 v[i].clear();//初始化vector 59 for (int i = 0; i < n; i ++) 60 { 61 sum = 0; 62 for (int j = 0; j < 6; j ++) 63 { 64 scanf("%d",&arr[i][j]); 65 sum += arr[i][j]; 66 } 67 int t = sum%p; 68 if (!flag)//没有找到相同的两组 69 { 70 for (int j = 0; j < v[t].size(); j ++) 71 { 72 if ((s_ee(v[t][j].id,i))||ee_s(v[t][j].id,i)) 73 { 74 flag = 1; 75 break; 76 } 77 } 78 if (!flag) 79 { 80 v[t].push_back((node){sum,i});//放到邻接表里 81 } 82 } 83 } 84 if (flag) 85 printf("Twin snowflakes found.\n"); 86 else 87 printf("No two snowflakes are alike.\n"); 88 return 0; 89 }