UVALive 6124 Hexagon Perplexagon 题解

http://vjudge.net/problem/viewProblem.action?id=37480

2012 East Central Regional Contest
3
Problem C: Hexagon Perplexagon
A well known puzzle consists of 7 hexagonal pieces, each with the numbers 1 through 6 printed on the
sides. Each piece has a different arrangement of the numbers on its sides, and the object is to place the
7 pieces in the arrangement shown below such that the numbers on each shared edge of the arrangement
are identical. Figure (a) is an example of one solution:
(a) Example Solution (b) Position Notation for Output
Rotating any solution also gives another trivially identical solution. To avoid this redundancy, we will
only deal with solutions which have a 1 on the uppermost edge of the central piece, as in the example.
Input
The first line of the input file will contain a single integer indicating the number of test cases. Each case
will consist of a single line containing 42 integers. The first 6 represent the values on piece 0 listed in
clockwise order; the second 6 represent the values on piece 1, and so on.
Output
For each test case, output the case number (using the format shown below) followed by either the phrase
No solution
or by a solution specification. A solution specification lists the piece numbers in the order
shown in the Position Notation of Figure (b). Thus if piece 3 is in the center, a 3 is printed first; if
piece 0 is at the top, 0 is printed second, and so on. Each test case is guaranteed to have at most one
solution.
Sample Input
2
3 5 6 1 2 4 5 1 2 3 6 4 2 3 5 4 1 6 3 1 5 6 2 4 5 4 1 3 6 2 4 2 3 1 5 6 3 6 1 2 4 5
6 3 4 1 2 5 6 4 3 2 5 1 6 5 3 2 4 1 5 4 6 3 2 1 2 5 6 1 4 3 4 6 3 5 2 1 1 3 5 2 6 4
Sample Output
Case 1: 3 0 5 6 1 4 2
Case 2: No solution
题目

 

题目是给你7个图中的六边形,每个六边形每个边有一个数字,也就是给你六七四十二个数字。然后这些六边形可以转,你要把7个六边形拼到一起,还符合每条边的数字相同的规则。

题解:

就是深搜啊!先放中间的六边形,然后根据中间的这些数字就能确定其他六边形的方向,然后放上去再看看相邻的两个有没有冲突,有冲突就回溯继续找,没冲突就继续找解。哐哐哐就是深搜,当然好像7重循环来撸也行。我深搜写得飞起来,要把这些转来转去的简直难。

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<vector>
 4 #include<algorithm>
 5 #include<cstring>
 6 //#include<conio.h>
 7 using namespace std;
 8 typedef long long ll;
 9 
10 struct six
11 {
12     int a[6];
13     int pos[7];
14 };
15 
16 int t,n;
17 six a[7];
18 int no[7];
19 int up[7];
20 bool used[7];
21 int getup(int x)
22 {
23     if(x==0) return a[no[x]].pos[1];
24     int edge=a[no[0]].a[(up[0]+x-1)%6];
25     int pose=a[no[x]].pos[edge];
26     int pedge=(x+2)%6;
27     return (pose-pedge+6)%6;
28 }
29 
30 bool gank(int x)
31 {
32     if(x<2) return false;
33     //cout<<no[x]<<'.'<<(up[x]+x+3)%6<<"="<<a[ no[x] ].a[ (up[x]+x+3)%6 ]<<"   ";
34     //cout<<no[x-1]<<'.'<<(up[x-1]+x)%6<<'='<<a[ no[x-1] ].a[ (up[x-1]+x)%6 ]<<endl;
35     if(a[ no[x] ].a[ (up[x]+x+3)%6 ] != a[ no[x-1] ].a[ (up[x-1]+x)%6 ]) return true;
36     //cout<<"false!";
37     if(x==6 && a[ no[x] ].a[ (up[x]+1)%6 ] != a[ no[1] ].a[ (up[1]+4)%6 ]) return true;
38     return false;
39 }
40 
41 bool dfs(int x)
42 {
43 //    printf("x=%d ",x);
44 //    for(int i=0;i<x;i++)
45 //        printf("%d,",no[i]);
46 //    printf("\n");
47 //    printf("up  ",x);
48 //    for(int i=0;i<x;i++)
49 //        printf("%d,",up[i]);
50 //    printf("\n");
51     //getch();
52     if(x==7)
53     {
54         return true;
55     }
56     for(int i=0;i<7;i++)
57         if(!used[i])
58         {
59             no[x]=i;
60             up[x]=getup(x);
61             if(gank(x)) continue;
62             used[i]=true;
63             if(dfs(x+1)) return true;
64             used[i]=false;
65         }
66     return false;
67 }
68 
69 int main()
70 {
71     int i,j,x;
72     scanf("%d",&n);
73     for(t=1;t<=n;t++)
74     {
75         for(i=0;i<7;i++)
76             for(j=0;j<6;j++)
77             {
78                 scanf("%d",&x);
79                 a[i].a[j]=x;
80                 a[i].pos[x]=j;
81             }
82         memset(used,false,sizeof(used));
83         printf("Case %d:",t);
84         if(dfs(0))
85         {
86             for(i=0;i<7;i++)
87                 printf(" %d",no[i]);
88             puts("");
89         }
90         else printf(" No solution\n");
91     }
92     return 0;
93 }
View Code

 

posted @ 2014-06-07 22:52  带鱼Yuiffy  阅读(383)  评论(0)    收藏  举报