密码游戏

时间:2019-11-06
本文章向大家介绍密码游戏,主要包括密码游戏使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int a[30],b[30];
bool aa[30],bb[30];
int n,m,cnt,dx,dy;
int head[50];
int x[100005],y[100005];
bool key;
int re[30][30];
long long read(){
    long long a=0,b=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        ch=getchar();
    }
    if(ch=='-'){
        b=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        a=a*10+ch-'0';
        ch=getchar();
    }
    return a*b;
}
void Print_res(){
    for (int i=0;i<m;i++){
        printf("%d ",a[i]);
    }
    printf("\n");
    for (int i=0;i<m;i++){
        printf("%d ",b[i]);
    }
}
void dfs(int t){
    if(t==m){
        Print_res();
        key=1;
        return;
    }
    for(int k=0;k<m;k++){
        if(aa[k]){
            continue;
        }
        a[t]=k;
        bool ok=1;
        bool temp[30]={0};
        for(int e=0;e<m;e++){
            if(re[t][e]==-1){
                continue;
            }
            int v=(a[t]+e)%m;
            int val=re[t][e];
            if((b[v]==-1&&bb[val]==0)||b[v]==val);
            else{
                ok=0;
                break;
            }
        }
        if(ok==0){
            continue;
        }
        aa[k]=1;
        for (int e=0;e<m;e++){
            if(re[t][e]==-1){
                continue;
            }
            int v=(a[t]+e)%m;
            int val=re[t][e];
            if(b[v]==-1){
                b[v]=val;
                bb[val]=1;
                temp[v]=1;
            }
        }
        dfs(t+1);
        if(key){
            return;
        }
        for (int i=0;i<m;i++){
            if (temp[i]){
                bb[b[i]]=0;
                b[i]=-1;
            }
        }
        aa[k]=0;
    }
}
int main(){
    freopen("password.in","r",stdin);
    freopen("password.out","w",stdout);
    n=read(),m=read();
    for(int i=1;i<=n;i++){
        x[i]=read();
    }
    for(int i=1;i<=n;i++){
        y[i]=read();
    }
    for(int i=0;i<m;i++){
        a[i]=b[i]=-1;
    }
    memset(re,-1,sizeof(re));
    while(cnt<n){
        cnt++;
        re[(x[cnt]+dx)%m][dy]=y[cnt];
        dx++;
        if(dx==m){
            dx=0,dy++;
        }
        if(dy==m){
            dy=0;
        }
    }
    dfs(0);
    return 0;
}

原文地址:https://www.cnblogs.com/xiongchongwen/p/11806070.html