Title Description
There are n different sizes of hollow discs, numbered from 1 to n in order from small to large. The n disks are arbitrarily iterated on three columns, and the columns are numbered a, B and C, which is called the initial state.
It is now required to find a mobile scheme with the least number of steps to transition from the initial state to the target State.
When moving, there are the following requirements:
• Only one disk can be moved at a time;
• Do not allow the market to move to the small plate.
Input/output format
Input format:
The first line of the file is the total number of disks in the State;
The second to the fourth row is the initial state of a, B, c column on the number of discs and from top to bottom each disk number;
The five to seventh lines are the number of discs on the A, B, and C bars in the target State, and the numbering of each disc from top to bottom respectively.
Output format:
One-step move scenario for each line, in the form: Move I from P to Q
The last line outputs a minimum number of steps.
Input/Output sample
Input Sample # #:
5
3 3 2 1
2 5 4
0
1 2
3 5 4 3
1 1
Sample # # of output:
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
Description
Number of discs ≤45
The first thing to do is to move the nth plate from X to Y
so that the smaller than n plates are all moved to 6-x-y
and the n is moved to Y.
Code
#include <iostream> #include <cstdio> #include <cstring> using namespace std;
int a[101],b[101],n;
Long long ans;
void Dfs (int x,int dep) {if (A[X]==DEP) return;
for (int i=x-1;i>=1;i--) DFS (i,6-dep-a[x]);
printf ("Move%d from%c to%c\n", x,a[x]+64,dep+64);
A[X]=DEP;
ans++;
} int main () {int x,d;
cin>>n;
cin>>x;
for (int i=1;i<=x;i++) {scanf ("%d", &d);
A[d]=1;
} cin>>x;
for (int i=1;i<=x;i++) {scanf ("%d", &d);
a[d]=2;
} cin>>x;
for (int i=1;i<=x;i++) {scanf ("%d", &d);
a[d]=3;
} cin>>x;
for (int i=1;i<=x;i++) {scanf ("%d", &d);
B[d]=1;
} cin>>x;
for (int i=1;i<=x;i++) {scanf ("%d", &d);
b[d]=2;
} cin>>x;
for (int i=1;i<=x;i++) {scanf ("%d", &d);
b[d]=3; } for (inti=n;i>=1;i--) DFS (I,b[i]);
cout<<ans; }