以下是全代码:
#include<bits/stdc++.h>
using namespace std;
int n,ton;
int as[30];
int to[30];
char in[30];
bool into[30];
bool used[30];
int a[30],b[30],c[30];
bool cerc(){
if(as[to[0]]==7&&as[to[1]]==2&&as[to[2]]==0&&as[to[3]]==8&&as[to[4]]==4
&&as[to[5]]==9 /*&&as[to[6]]==1&&as[to[7]]==3&&as[to[8]]==5&&as[to[9]]==6*/){
return 1;
}
return 0;
}
bool check(){
if(as[a[1]]+as[b[1]] >= n){
//if(cerc()) printf("end by begin\n",1);
return 0;
}
for(int i=n;i>=1;i--){
if(as[a[i]]!=-1 && as[b[i]]!=-1 && as[c[i]]!=-1){
if((as[a[i]]+as[b[i]])%n != as[c[i]]
&& (as[a[i]]+as[b[i]]+1)%n != as[c[i]]){
//if(cerc()) printf("end by %d\n",i);
return 0;
}
}
}
if(as[a[n]]!=-1 && as[b[n]]!=-1 && as[c[n]]!=-1){
if((as[a[n]]+as[b[n]])%n != as[c[n]]){
//if(cerc()) printf("end by end\n",n);
return 0;
}
}
return 1;
}
bool checked(){
int up=0;
for(int i=n;i>=1;i--){
/*if(i==1){
printf("%c+%c+...=%c?\n",a[i],b[i],c[i]);
printf("%d+%d+%d=%d?\n",as[a[i]],as[b[i]],up,as[c[i]]);
}*/
if((as[a[i]]+as[b[i]]+up)%n != as[c[i]]){
//printf("end by %d\n",i);
return 0;
}
up = (as[a[i]]+as[b[i]]+up)/n;
}
return 1;
}
void dfs(int now){
if(!check()) return ;
if(now == n){
if(checked()){
for(int i=0;i<n;i++){
printf("%d ",as[i]);
}
exit(0);
}
return ;
}
for(int i=n-1;i>=0;i--){
if(!used[i]){
as[to[now]] = i;
used[i] = 1;
dfs(now+1);
used[i] = 0;
as[to[now]] = -1;
}
}
}
void pus(int x){
if(!into[x]){
into[x] = 1;
to[ton] = x;
ton++;
}
}
int main(){
memset(as,-1,sizeof(as));
scanf("%d",&n);
scanf("%s",in+1);
for(int i=1;i<=n;i++){
a[i] = in[i]-'A';
}
scanf("%s",in+1);
for(int i=1;i<=n;i++){
b[i] = in[i]-'A';
}
scanf("%s",in+1);
for(int i=1;i<=n;i++){
c[i] = in[i]-'A';
}
for(int i=n;i>=1;i--){
pus(a[i]);
pus(b[i]);
pus(c[i]);
}
dfs(0);
return 0;
}
把66行从:
for(int i=n-1;i>=0;i--){
改成:
for(int i=0;i<n;i++){
就会TLE
AC的写法是否比改后的写法有更大的几率剪枝或遇到答案?求神犇解答
(昨天写贴子时写代码写晕了,因此两行代码搞错了,已紫衫)