- 汉诺塔问题 move 1 from A to B
- 2024-08-05 23:25:21 @
#include<bits/stdc++.h>
using namespace std;
int n,now[50],la[50],cnt;string pan="0ABC";
void move(int x,int y)
{
if(now[x]==y)return;
for(int i=x-1;i>=1;i--)
move(i,6-(now[x]+y));
printf("move %d from %c to %c\n",x,pan[now[x]],pan[y]);
now[x]=y,cnt++;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
//ios::sync_with_stdio(false);
/*
只能用freopen看出来
加了ios::sync_with_stdio(false);
ans:
7
move 1 from A to B
move 2 from A to C
move 1 from B to C
move 3 from A to B
move 1 from C to B
move 2 from C to A
move 1 from B to C
不加
ans:
move 1 from A to B
move 2 from A to C
move 1 from B to C
move 3 from A to B
move 1 from C to B
move 2 from C to A
move 1 from B to C
7
????
*/
cin>>n;
for(int i=1;i<=3;i++)
{
int p;cin>>p;
for(int j=1;j<=p;j++)
{
int a;cin>>a;
now[a]=i;
}
}
for(int i=1;i<=3;i++)
{
int p;cin>>p;
for(int j=1;j<=p;j++)
{
int a;cin>>a;
la[a]=i;
}
}
for(int i=n;i>=1;i--)
move(i,la[i]);
cout<<cnt<<endl;
return 0;
}
1 条评论
-
30201沈俊彦 (2212116沈俊彦) LV 10 @ 2024-08-06 16:31:01
是关于printf和cout的问题,但我不知道具体怎么说
- 1
信息
- ID
- 1548
- 难度
- 6
- 分类
- (无)
- 标签
- 递交数
- 26
- 已通过
- 10
- 通过率
- 38%
- 被复制
- 2
- 上传者