【jzoj3741】【TJOI2014】【拼图(puzzle)】【搜索】

题目大意

【jzoj3741】【TJOI2014】【拼图(puzzle)】【搜索】_第1张图片

解题思路

把填放状态压成二进制,直接搜索即可。

code

#include
#include
#include
#include
#include
#define LF double
#define LL long long
#define Min(a,b) ((a
#define Max(a,b) ((a>b)?a:b)
#define Fo(i,j,k) for(int i=j;i<=k;i++)
#define Fd(i,j,k) for(int i=j;i>=k;i--)
#define For(i,j) for(int i=Begin[j];i;i=Next[i])
using namespace std;
int const Mxn=20;
int N,Ans0,R[Mxn],C[Mxn],S[Mxn],A[Mxn],X[Mxn],Y[Mxn],AnsA[Mxn],AnsX[Mxn],
    AnsY[Mxn],Ans[10][10],Two[20];
void Dfs(int Now,int Sta){
    if(Sta==65535){
        Ans0++;
        Fo(i,0,A[0])AnsA[i]=A[i],AnsX[i]=X[i],AnsY[i]=Y[i];
    }
    if(Now>N)return;
    if(Ans0>1)return;
    A[++A[0]]=Now;
    Fo(i,1,4-R[Now]+1)Fo(j,1,4-C[Now]+1)
        if(!(Sta&(S[Now]*Two[(5-i-R[Now])*4+5-j-C[Now]]))){
            X[A[0]]=i;Y[A[0]]=j;
            Dfs(Now+1,Sta|(S[Now]*Two[(5-i-R[Now])*4+5-j-C[Now]]));
            if(Ans0>1){A[0]--;return;}
        }
    A[0]--;
    Dfs(Now+1,Sta);
}
int main(){
    //freopen("puzzle.in","r",stdin);
    //freopen("puzzle.out","w",stdout);
    freopen("d.in","r",stdin);
    freopen("d.out","w",stdout);
    Two[0]=1;Fo(i,1,16)Two[i]=Two[i-1]<<1;
    while(scanf("%d",&N)!=EOF){
        Fo(i,1,N){
            scanf("%d%d\n",&R[i],&C[i]);S[i]=0;
            Fo(j,1,R[i]){
                Fo(k,C[i]+1,4)S[i]=S[i]*2;
                Fo(k,1,C[i]){
                    char Ch;scanf("%c",&Ch);
                    S[i]=S[i]*2+Ch-'0';
                }
                scanf("\n");
            }
        }
        Ans0=0;
        Dfs(1,0);
        if(!Ans0)printf("No solution\n");
        else if(Ans0>1)printf("Yes, many!\n");
        else{
            Fo(i,1,AnsA[0])Fd(j,R[AnsA[i]],1){
                Fd(k,C[AnsA[i]],1){
                    if(S[AnsA[i]]&1)Ans[AnsX[i]+j-1][AnsY[i]+k-1]=AnsA[i];
                    S[AnsA[i]]>>=1;
                }
                Fo(K,C[AnsA[i]]+1,4)S[AnsA[i]]>>=1;
            }
            printf("Yes, only one!\n");
            Fo(i,1,4){
                Fo(j,1,4)printf("%d",Ans[i][j]);
                printf("\n");
            }
        }
    }
    return 0;
}

你可能感兴趣的:(jzoj,搜索)