luogu_1092 蟲食算
阿新 • • 發佈:2017-10-05
%d std char s include meta logs stream -- void
#include <iostream> #include <cstring> #include <cstdio> using namespace std; const int N=30; int n,s1[4][N],num[N]; char s[4][N]; bool vis[N]; void dfs(int x,int y,int t){ if(x==0){ if(t==0){ for(int i=1;i<=n;i++)printf("%d ",num[i]); puts(""); exit(0); } return; } for(int i=x-1;i>=1;i--){ int w1=num[s1[1][i]],w2=num[s1[2][i]],w3=num[s1[3][i]]; if(w1==-1 || w2==-1 || w3==-1)continue; if((w1+w2)%n!=w3 && (w1+w2+1)%n!=w3)return; } if(num[s1[y][x]]==-1){ for(int i=n-1;i>=0;i--){ if(!vis[i]){ if(y!=3){ num[s1[y][x]]=i; vis[i]=1; dfs(x,y+1,t); num[s1[y][x]]=-1; vis[i]=0; } else{ int w=num[s1[1][x]]+num[s1[2][x]]+t; if(w%n!=i)continue; vis[i]=1; num[s1[3][x]]=i; dfs(x-1,1,w/n); vis[i]=0; num[s1[3][x]]=-1; } } } } else { if(y!=3)dfs(x,y+1,t); else { int w=num[s1[1][x]]+num[s1[2][x]]+t; if(w%n!=num[s1[3][x]])return; dfs(x-1,1,w/n); } } } int main(){ scanf("%d",&n); for(int i=1;i<=3;i++)scanf("%s",s[i]+1); for(int i=1;i<=n;i++)s1[1][i]=s[1][i]-‘A‘+1; for(int i=1;i<=n;i++)s1[2][i]=s[2][i]-‘A‘+1; for(int i=1;i<=n;i++)s1[3][i]=s[3][i]-‘A‘+1; memset(num,0xff,sizeof(num)); memset(vis,0,sizeof(vis)); dfs(n,1,0); return 0; }
luogu_1092 蟲食算