记录编号 452979 评测结果 AAAAAAAAAA
题目名称 [NOIP 2004]虫食算 最终得分 100
用户昵称 GravatarRegnig Etalsnart 是否通过 通过
代码语言 C++ 运行时间 0.769 s
提交时间 2017-09-20 19:00:43 内存使用 0.06 MiB
显示代码纯文本
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int n,num[27],vis[27],ok,i;
int pic[4][27],p[27];
string s1,s2,s3;
bool check()
{
	for(int j=1;j<=n;j++)
		if((num[pic[1][j]]+num[pic[2][j]]+p[j])%n!=num[pic[3][j]])return 0;
	return 1;
}
void dfs(int now)
{
	if(ok)return;
	if(check())
	{
		for(int j=1;j<=n;j++)printf("%d ",num[j]);
		printf("\n");
		ok=1;
		return;
	}
	int judger=(num[pic[1][now]]==-1)+(num[pic[2][now]]==-1)+(num[pic[3][now]]==-1);
    if(judger==0)
	{
		int flag=0;
		if(((num[pic[1][now]]+num[pic[2][now]]+p[now])%n)!=num[pic[3][now]])return;
		if(num[pic[1][now]]+num[pic[2][now]]+p[now]>=n)
		{
			p[now-1]=1;
			flag=1;	
		}
		dfs(now-1);
		if(flag)p[now-1]=0;
	}
    else if(judger==1)
	{
		if(num[pic[1][now]]==-1)
		{
			int res=num[pic[3][now]]-num[pic[2][now]]-p[now];
			if(res<0)res+=n;
			if(vis[res])return;
			vis[res]=1;
			num[pic[1][now]]=res;
			dfs(now);
			num[pic[1][now]]=-1;
			vis[res]=0;
		}
		else if(num[pic[2][now]]==-1)
		{
			int res=num[pic[3][now]]-num[pic[1][now]]-p[now];
			if(res<0)res+=n;
			if(vis[res])return;
			vis[res]=1;
			num[pic[2][now]]=res;
			dfs(now);
			num[pic[2][now]]=-1;
			vis[res]=0;
		}
		else
		{
			int res=num[pic[1][now]]+num[pic[2][now]]+p[now];
			if(res>=n)res-=n;
			if(vis[res])return;
			vis[res]=1;
			num[pic[3][now]]=res;
			dfs(now);
			num[pic[3][now]]=-1;
			vis[res]=0;
		}
	}
    else if(judger==2)
    {
    	for(int j=n-1;j>=0;j--)if(!vis[j])if(!ok)
    	{
    		if(~num[pic[1][now]])
    		{
    			vis[j]=1;
    			num[pic[2][now]]=j;
    			dfs(now);
    			num[pic[2][now]]=-1;
    			vis[j]=0;
			}
			else
			{
				vis[j]=1;
    			num[pic[1][now]]=j;
    			dfs(now);
    			num[pic[1][now]]=-1;
    			vis[j]=0;
			}
		}
	}
    else
    {
    	for(int j=n-1;j>=0;j--)if(!vis[j])if(!ok)
    	{
    		vis[j]=1;
    		num[pic[1][now]]=j;
    		dfs(now);
    		num[pic[1][now]]=-1;
    		vis[j]=0;
		}
	}
	return;
}
int Main()
{
	freopen("alpha.in","r",stdin);freopen("alpha.out","w",stdout);
    scanf("%d",&n);
    cin>>s1>>s2>>s3;
    memset(num,-1,sizeof(num));
    for(i=0;i<n;i++)pic[1][i+1]=s1[i]-'A'+1;
    for(i=0;i<n;i++)pic[2][i+1]=s2[i]-'A'+1;
    for(i=0;i<n;i++)pic[3][i+1]=s3[i]-'A'+1;
    dfs(n);
    return 0;
}
int main(){;}
int syy=Main();