听说正解是高斯消元,但我还是选择搜索
考虑从右往左搜
退出条件是搜完所有位置
剪枝好像只能可行性剪枝
复杂度还是很高
枚举的时候还能再优化一下
- 三个数均未确定
- 枚举其中两个计算第三个
- 两个数未确定
- 枚举一个计算一个
- 一个数未确定
- 直接计算
- 均确定
- 可行性剪枝
比较容易忽略的是回溯过程
不合法也要先回溯再退出
最慢点1000+ms,吸口氧就能过了1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const int N=500;
int n,x[N],y[N],z[N],w[N],used[N],f[N];
inline int read()
{
register int x=0,t=1;
register char ch=getchar();
while ((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
if (ch=='-') {t=-1;ch=getchar();}
while (ch>='0'&&ch<='9') {x=x*10+ch-48;ch=getchar();}
return x*t;
}
inline char get()
{
register char ch=getchar();
while (!('A'<=ch&&ch<='Z')) ch=getchar();
return ch;
}
void print()
{
for(int i=0;i<n;i++)
printf("%d ",w['A'+i]);
}
void dfs(int t,int d)
{
if (t<=0)
{
print();
exit(0);
}
if (f[x[t]]&f[y[t]]&f[z[t]])
{
if ((w[x[t]]+w[y[t]]+d)%n==w[z[t]])
dfs(t-1,(w[x[t]]+w[y[t]]+d)/n);
return;
}
for(int fx=0,i=n-1;i>=0&&!fx;i--) //1
{
if (f[x[t]]) fx=1; //Already
if (!fx&&f[y[t]]&f[z[t]]) //2&3
{
fx=1;
int val=(w[z[t]]+n-d-w[y[t]])%n;
if (!used[val])
{
f[x[t]]=used[w[x[t]]=val]=1;
dfs(t-1,(w[x[t]]+w[y[t]]+d)/n);
f[x[t]]=used[val]=0;
}
}
if (!fx&&!used[i]) f[x[t]]=used[w[x[t]]=i]=1;
for(int fy=0,j=n-1;j>=0&&!fy;j--) if (f[x[t]])
{
if (f[y[t]]) fy=1; //Already
if (!fy&&f[z[t]]) //1&3
{
fy=1;
int val=(w[z[t]]+n-d-w[x[t]])%n;
if (!used[val])
{
f[y[t]]=used[w[y[t]]=val]=1;
dfs(t-1,(w[x[t]]+w[y[t]]+d)/n);
f[y[t]]=used[val]=0;
}
}
if (!fy&&!used[j]) f[y[t]]=used[w[y[t]]=j]=1;
if (f[y[t]])
{
int fz=0,val=(w[x[t]]+w[y[t]]+d)%n;
if (f[z[t]]) fz=1; //Already
if (!fz&&!used[val]) f[z[t]]=used[w[z[t]]=val]=1;
if ((w[x[t]]+w[y[t]]+d)%n==w[z[t]])
if (f[z[t]]) dfs(t-1,(w[x[t]]+w[y[t]]+d)/n);
if (!fz&&f[z[t]]) f[z[t]]=used[val]=0;
}
if (!fy&&f[y[t]]) f[y[t]]=used[j]=0;
}
if (!fx&&f[x[t]]) f[x[t]]=used[i]=0;
}
}
int main()
{
n=read();
for(int i=1;i<=n;i++) x[i]=get();
for(int i=1;i<=n;i++) y[i]=get();
for(int i=1;i<=n;i++) z[i]=get();
dfs(n,0);
return 0;
}