100642D GYM

85人阅读 评论(0)

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 di erent 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 rst line of the input le will contain a single integer indicating the number of test cases. Each case
will consist of a single line containing 42 integers. The rst 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 speci cation. A solution speci cation 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 rst; 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

#include <set>
#include <map>
#include <queue>
#include <deque>
#include <cstdio>
#include <string>
#include <vector>
#include <math.h>
#include <time.h>
#include <utility>
#include <cstdlib>
#include <sstream>
#include <cstring>
#include <stdio.h>
#include <iomanip>
#include <iostream>
#include <algorithm>
using namespace std;
const int dx[]={0,3,4,5,6,1,2},dy[]={0,5,6,1,2,3,4};
const int pos[]={0,4,5,6,1,2,3};
int n,stamp,T;
int used[8],b[8];
struct round{//还可以这么用
int a[10];
void scan()
{
for(int i=1;i<=6;i++)
cin>>a[i];
}
void rot()
{
int temp=a[1];
for(int i=1;i<6;i++)
a[i]=a[i+1];
a[6]=temp;
}
}a[8],t[8];
bool dfs(int d)
{
if(d==7)
{
for(int i=0;i<7;i++)
cout<<b[i]<<" ";
cout<<endl;
return true;
}
for(int i=0;i<7;i++)
{
if(used[i])continue;
t[d]=a[i];
b[d]=i;
if(d==0)
while(t[0].a[1]!=1)t[0].rot();
else
{
while(t[d].a[pos[d]]!=t[0].a[d])t[d].rot();
if(d==6&&t[d].a[dx[d]]!=t[1].a[dy[1]])continue;
if(d>1&&t[d].a[dy[d]]!=t[d-1].a[dx[d-1]])continue;
}
used[i]=1;
if(dfs(d+1))return true;
used[i]=0;
}
return false;
}
int main()
{
cin>>T;
for(int i=1;i<=T;i++)
{
memset(used,0,sizeof(used));
for(int i=0;i<7;i++)
{
a[i].scan();
}
printf("Case %d: ",++stamp);
if(!dfs(0))
{
cout<<"No solution"<<endl;
}
}
return 0;
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：2686次
• 积分：303
• 等级：
• 排名：千里之外
• 原创：29篇
• 转载：1篇
• 译文：0篇
• 评论：0条
文章分类
文章存档