Hexagon Perplexagon

原文链接: http://www.cnblogs.com/liulangye/archive/2013/01/31/2886830.html

http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1945

题目不难 dfs 就可以 不过后台数据相当的大 所以要尽量优化

我是边搜边判 

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
#include<vector>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<algorithm>
#include<cmath>
#define LL long long
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
const int INF=0x3f3f3f3f;
//const int MOD=1000000009;
const int N=35;
int a[N][N],f[N][N];
int k[N],l[N];
bool had[N];
bool hadans;
void dfs(int x)
{
    for(int i=0;i<7;++i)
    if(!had[i])
    {
        k[x]=i;
        if(x==0)
        {
            l[0]=f[k[0]][1];
        }else
        {
            int t=a[k[0]][l[0]+x-1];
            l[x]=f[k[x]][t];
            if(x>1&&a[k[x-1]][l[x-1]+5]!=a[k[x]][l[x]+1])
            continue;
            if(x==6&&a[k[x]][l[x]+5]==a[k[1]][l[1]+1])
            hadans=true;
        }
        had[i]=true;
        if(x<6)
        dfs(x+1);
        had[i]=false;
        if(hadans)
        return ;
    }
}
int main()
{
    //freopen("data.in","r",stdin);
    int T;
    scanf("%d",&T);
    int ca=1;
    while(T--)
    {
        for(int i=0;i<7;++i)
        for(int j=0;j<6;++j)
        {
            scanf("%d",&a[i][j]);
            a[i][j+6]=a[i][j];
            f[i][a[i][j]]=j;
        }
        hadans=false;
        memset(had,false,sizeof(had));
        dfs(0);
        printf("Case %d:",ca++);
        if(hadans)
        {
            for(int i=0;i<7;++i)
            printf(" %d",k[i]);

        }else
        printf(" No solution");
        printf("\n");
    }
    return 0;
}

  

转载于:https://www.cnblogs.com/liulangye/archive/2013/01/31/2886830.html

猜你喜欢

转载自blog.csdn.net/weixin_30815469/article/details/94791341
今日推荐