什么会致错?
查看原帖
什么会致错?
209944
君玘楼主2020/11/9 21:23

我就暴力打dfs,因为我也不会剪枝。。。但是有错的,我也不知道为什么,请dalao指教。

#include<bits/stdc++.h>
#define gc ch=getchar()
#define pc putchar(32)
#define pt puts("")
#define aA ans[A[k]]
#define aB ans[B[k]]
#define aC ans[C[k]]
#define equ (aA+aB+cy)
using namespace std;
template <class T>void read(T &s){
   s=0;T f=1;char gc;
   while(ch<'0'||ch>'9'){if(ch=='-')f=-1;gc;}
   while(ch>='0'&&ch<='9'){s=s*10+ch-'0';gc;}
   s*=f;	
}
template <class T>void put(T s){
   if(s<0) putchar('-'),s=-s;
   if(s>9) put(s/10);
   putchar(s%10+'0');
}
int n,ans[30],A[30],B[30],C[30],vis[30],flag;
void dfs(int k,int cy){
   if(flag) return;
   if(k>n){
   	if(cy) return;
   	for(int i=1;i<=n;++i) put(ans[i]),pc;
   	flag=1;
   	return;
   }
   if(aA>=0&&aB>=0&&aC>=0){
   	if(equ%n==aC){
   		dfs(k+1,equ/n);
   	} 
   	return;
   }
   if(aA<0&&aB<0&&aC<0){
   	for(int i=n-1;i>=0;--i){
   		if(vis[i]) continue;
   		aA=i,vis[i]=1;
   		for(int j=n-1;j>=0;--j){
   			if(vis[j]||vis[(i+j+cy)%n]||(i+j+cy)%n==j) continue;
   			aB=j,vis[j]=1;
   			aC=equ%n,vis[aC]=1;
   			dfs(k+1,equ/n);
   			vis[aC]=vis[j]=0;
   			aB=aC=-1;
   		}
   		aA=-1,vis[i]=0;
   	}
   }
   if(aA>=0&&aB>=0&&aC<0){
   	if(vis[equ%n]) return;
   	aC=equ%n,vis[aC]=1;
   	dfs(k+1,equ/n);
   	vis[aC]=0,aC=-1;
   	return;
   }
   if(aA>=0&&aB<0&&aC<0){
   	for(int i=n-1;i>=0;--i){
   		if(vis[i]||vis[(i+aA+cy)%n]||(i+aA+cy)%n==i) continue;
   		aB=i,vis[i]=1;
   		aC=equ%n,vis[aC]=1;
   		dfs(k+1,equ/n);
   		vis[aC]=vis[i]=0;
   		aB=aC=-1;
   	}
   	return;
   }
   if(aB>=0&&aA<0&&aC<0){
   	for(int i=n-1;i>=0;--i){
   		if(vis[i]||vis[(i+aB+cy)%n]||(i+aB+cy)%n==i) continue;
   		aA=i,vis[i]=1;
   		aC=equ%n,vis[aC]=1;
   		dfs(k+1,equ/n);
   		vis[aC]=vis[i]=0;
   		aA=aC=-1;
   	}
   	return;
   }
   if(aB<0&&aA>=0&&aC>=0){
   	if(aC-aA-cy>=0&&aC-aA-cy<n){
   		if(!vis[aC-aA-cy]){
   			aB=aC-aA-cy,vis[aB]=1;
   			dfs(k+1,equ/n);
   			vis[aB]=0,aB=-1;
   		}
   	}
   	if(aC-aA-cy+n>=0&&aC-aA-cy<0){
   		if(!vis[aC-aA-cy+n]){
   			aB=aC-aA-cy+n,vis[aB]=1;
   			dfs(k+1,equ/n);
   			vis[aB]=0,aB=-1;
   		}
   	}
   	return;
   }
   if(aA<0&&aB>=0&&aC>=0){
   	if(aC-aB-cy>=0&&aC-aB-cy<n){
   		if(!vis[aC-aB-cy]){
   			aA=aC-aB-cy,vis[aA]=1;
   			dfs(k+1,equ/n);
   			vis[aA]=0,aA=-1;
   		}
   	}
   	if(aC-aB-cy+n>=0&&aC-aB-cy<0){
   		if(!vis[aC-aB-cy+n]){
   			aA=aC-aB-cy+n,vis[aA]=1;
   			dfs(k+1,equ/n);
   			vis[aA]=0,aA=-1;
   		}
   	}
   	return;
   }
   if(aC>=0&&aA<0&&aB<0){
   	for(int i=n-1;i>=0;--i){
   		if(vis[i]) continue;
   		aA=i,vis[i]=1;
   		if(aC-aA-cy>=0&&aC-aA-cy<n){
   			if(!vis[aC-aA-cy]){
   				aB=aC-aA-cy,vis[aB]=1;
   				dfs(k+1,equ/n);
   				vis[aB]=0,aB=-1;
   			}
   		}
   		if(aC-aA-cy+n>=0&&aC-aA-cy<0){
   			if(!vis[aC-aA-cy+n]){
   				aB=aC-aA-cy+n,vis[aB]=1;
   				dfs(k+1,equ/n);
   				vis[aB]=0,aB=-1;
   			}
   		}
   		aA=-1,vis[i]=0;
   	}
   	return;
   }
}
int main(){
   read(n);
   for(int i=1;i<=n;++i) ans[i]=-1;
   for(int i=1;i<4;++i){
   	char gc;
   	for(int j=n;j;--j){
   		gc;
   		if(i==1) A[j]=ch-'A'+1;
   		if(i==2) B[j]=ch-'A'+1;
   		if(i==3) C[j]=ch-'A'+1;
   	}
   	gc;
   }
   dfs(1,0);
}
2020/11/9 21:23
加载中...