100642D GYM

完全是抄的。。。搜索水平还要加强,现在跟小学生没什么区别。

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
先开始想的方法是枚举,通过全排列枚举顺序,再枚举旋转次数,这样的时间复杂度是O(7!*6*6*6*6*6*6*6),
只只要是no solution的情况就会跑一边,而且还是多测。。。一个no solution 跑了30s。。。
还还是算了吧。
看看见了一种靠谱的搜索方法:因为题目中说到了中间的方格一必须朝上,那么旋转即可(中间的方格不用枚举旋转次数)然后旋转其余的6个方格:这里只需要将其下面的数字和中间那块对齐即可,对齐之后如果不满足其他边界相等,则continue(这个格子放这个方块不可以,枚举下一个方块),在可以放置方块之后将used=1(*),
然后dfs下一个格子,返回值是bool,当这个格子能放下某一个方格则返回true,当放置了7个之后说明成功也返回trtrue,否则在枚举了所有的方格之后都不可以返回false,返回上一个格子,枚举上一个格子的其他方案,当枚举完第一个格子的所有方案时都不行,则退出递归。
时时间复杂度:一共有7!个状态数,每枚举出一个状态都要转6*6*6*6*6*6次,复杂度好像是一样的,但是跑得很快不到1s,大概是旋转的时候包含了剪枝,转的时候经常会对不齐。
从vjudge上抄的。。。自己弱的不行
代代码:来自vjudge
#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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值