题解 P1242 【新汉诺塔】
由于这道题加了数据之后除了模拟退火和打表基本无法通过
那么我来加一个不是模拟退火也不是打表的解法
有一个讨论说的很对,我们原先的正解是每次先把最大的移动到他该有的位置,这样能过掉绝大多数情况,但是有一种情况能把它卡掉。
就是
3
1 3
0
2 2 1
2 2 1
0
1 3
这种情况我们称为Hack情况,特征就是有一个空柱,可以先把最大的移动到空柱上面,然后再进行操作,有时会比前面的贪心更优。
我有一个猜想就是:Hack情况只能发生在初始状态
我们可以这么想,假入初始状态不是这种情况,那么我们就只能按原先的贪心来做,而贪心完的样子就是:
最大的那个单占一个柱子,一个空柱子,剩下的在最后一个柱子上
这种情况显然不是Hack情况,这样下去也就永远不会出现Hack情况了
所以我们可以看初始情况是不是Hack情况,然后是的话另开一个数组复制原来的情况,然后挪一下圆盘进行计算,最后比较一波大小就好了。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#define ll long long
#define N 100010
using namespace std;
int Goal[50],n;
int Astack[4][50],Atop[4],Ahead[4],Anow[50],Awrite[N][3],acnt;
int Bstack[4][50],Btop[4],Bhead[4],Bnow[50],Bwrite[N][3],bcnt;
ll Ans,Bns;
bool Change;
bool cmp(int x,int y) {
return x>y;
}
ll Adfs(int x,int to,int pos) {
ll ans=0; bool flag=0;
do {
flag=1;
if (pos!=Atop[Anow[x]]) {
ans+=Adfs(Astack[Anow[x]][pos+1],6-Anow[x]-to,pos+1);
flag=0;
}
int top=Atop[to];
if (Ahead[to]<=Atop[to]) {
while (Astack[to][top]<x && top>=Ahead[to]) top--;
if (top!=Atop[to]) {
ans+=Adfs(Astack[to][top+1],6-Anow[x]-to,top+1);
flag=0;
}
}
}
while (!flag);
Awrite[++acnt][0]=x;
Awrite[acnt][1]=Anow[x]+'A'-1;
Awrite[acnt][2]=to+'A'-1;
Atop[Anow[x]]--;
Anow[x]=to;
Astack[to][++Atop[to]]=x;
return ans+1;
}
ll Bdfs(int x,int to,int pos) {
ll bns=0; bool flag=0;
do {
flag=1;
if (pos!=Btop[Bnow[x]]) {
bns+=Bdfs(Bstack[Bnow[x]][pos+1],6-Bnow[x]-to,pos+1);
flag=0;
}
int top=Btop[to];
if (Bhead[to]<=Btop[to]) {
while (Bstack[to][top]<x && top>=Bhead[to]) top--;
if (top!=Btop[to]) {
bns+=Bdfs(Bstack[to][top+1],6-Bnow[x]-to,top+1);
flag=0;
}
}
}
while (!flag);
Bwrite[++bcnt][0]=x;
Bwrite[bcnt][1]=Bnow[x]+'A'-1;
Bwrite[bcnt][2]=to+'A'-1;
Btop[Bnow[x]]--;
Bnow[x]=to;
Bstack[to][++Btop[to]]=x;
return bns+1;
}
void Copy() {
memcpy(Bstack,Astack,sizeof(Astack));
memcpy(Bnow,Anow,sizeof(Anow));
memcpy(Bhead,Ahead,sizeof(Ahead));
memcpy(Btop,Atop,sizeof(Atop));
memcpy(Bwrite,Awrite,sizeof(Awrite));
}
void Check() {
while (Anow[n]==Goal[n] && n) {
Ahead[Anow[n]]++;
n--;
}
if (!n) {
puts("0");
exit(0);
}
int p=6-Anow[n]-Goal[n];
if (Ahead[p]>Atop[p] && Ahead[Anow[n]]==Atop[Anow[n]]) {
Copy();
Bwrite[++bcnt][0]=n;
Bwrite[bcnt][1]=Bnow[n]+'A'-1;
Bwrite[bcnt][2]=p+'A'-1;
Btop[Bnow[n]]--;
Bnow[n]=p;
Bstack[p][++Btop[p]]=n;
Change=1;
}
}
int main() {
scanf("%d",&n);
for (int i=1; i<=3; i++) {
scanf("%d",&Atop[i]);
Ahead[i]=1;
for (int j=1; j<=Atop[i]; j++) {
scanf("%d",&Astack[i][j]);
Anow[Astack[i][j]]=i;
sort(Astack[i]+1,Astack[i]+Atop[i]+1,cmp);
}
}
for (int i=1; i<=3; i++) {
int x,y; scanf("%d",&x);
for (int j=1; j<=x; j++) {
scanf("%d",&y);
Goal[y]=i;
}
}
Check();
for (int i=n; i>=1; i--) {
int g=Goal[i];
if (Anow[i]!=g) Ans+=Adfs(i,g,Ahead[Anow[i]]);
Ahead[g]++;
if (Change) {
if (Bnow[i]!=g)
Bns+=Bdfs(i,g,Bhead[Bnow[i]]);
Bhead[g]++;
}
}
if (Change && Bns+1<Ans) {
for (int i=1; i<=bcnt; i++)
printf("move %d from %c to %c\n",Bwrite[i][0],(char)(Bwrite[i][1]),(char)(Bwrite[i][2]));
printf("%lld",Bns+1);
}
else {
for (int i=1; i<=acnt; i++)
printf("move %d from %c to %c\n",Awrite[i][0],(char)(Awrite[i][1]),(char)(Awrite[i][2]));
printf("%lld",Ans);
}
}
/*
5
3 3 2 1
2 5 4
0
1 2
3 5 4 3
1 1
6
3 1 2 3
2 4 5
1 6
0
6 1 2 3 4 5 6
0
3
1 3
0
2 2 1
2 2 1
0
1 3
*/