Give N, say that there are n different sizes of plates, and then give the initial position and target location of each plate, the need to calculate the minimum number of steps so that each plate is moved to its target location.
Analysis: First find the biggest not on the target column of plate K, because if the largest plate on the target pillar it does not need to move, it is not in the way.
So the problem is to move K to the target column, the 1 to (K-1) moved to the relay column, so assume that K from a to b,a only k,b is empty, C above is K-1 to 1, the situation is referred to as the reference situation. Since the movement is symmetrical, moving from a reference to a target situation to a reference situation is the same as the number of steps.
So the question becomes the answer = moves from the initial situation to the reference situation step + the target situation moves to the reference situation step number +1;
A function f (p,i,final) is required to indicate that an array of initial cylinder numbers for a known plate is P, which moves 1 to I to the final step, the answer is F (start,k-1,6-start[k]-finish[k]) +f (finish,k-1,6- START[K]-FINISH[K]) +1;
Calculation f (p,i,final), if p[i]=final, then f (p,i,final) =f (p,i-1,final), otherwise you need to move the front i-1 plate to the transfer plate, the plate I moved to the final column, When you do, move the front i-1 plate from the turntable to the pillar final. The final step is to move the i-1 plate from one pillar to another, according to the old Hanoi tower problem, this step requires 2^ (I-1)-1 steps, plus move the plate I that step, altogether need 2^ (i-1) step.
F (p,i,final) =f (p,i-1,6-p[i]-final) +2^ (i-1);
#include <iostream>
#include <cstdio>
#include <cstring>
#define MAXN 100
#define LL Long Long
using namespace Std;
int ST[MAXN],ED[MAXN];
LL Fun (int *p,int i,int final)
{
if (i==0) return 0;
if (p[i]==final) return fun (p,i-1,final);
Return Fun (P,i-1,6-final-p[i]) + (1ll<< (i-1));
}
int main ()
{
int n;
int Cas=1;
while (~SCANF ("%d", &n) &&n)
{
for (int i=1; i<=n; i++)
scanf ("%d", &st[i]);
for (int i=1; i<=n; i++)
scanf ("%d", &ed[i]);
int k=n;
while (st[k]==ed[k]&&k) k--;
LL ans=0;
if (k!=0)
{
int tem=6-st[k]-ed[k];
Ans=fun (St,k-1,tem) +fun (Ed,k-1,tem) +1;
}
printf ("Case%d:%lld\n", Cas++,ans);
}
return 0;
}
UVa 10795-a Different Task