The main problem: is the ordinary question of the Nottingham, given N, said that there are n different sizes of dishes, and then give the initial position and target location of each dish, the need to calculate the minimum number of steps so that each plate is moved to its target location.
Idea: Consider the plate with the largest number, if it is in the initial position and the target situation on the same pillar, then we do not need to move it.
Because the plate movement is reversible, according to symmetry, we only need to ask for the initial situation and the target situation to move the reference situation of the sum of steps, and then add one.
We can write a function f (P, I, final) that indicates that the number of steps required to move the 1,2,3....I to the final column is known as the initial number of the plates, and the answer to this question is F (Start, k-1, 6-start[k]-start[k]) +f ( Finish, K-1, 6-start[k]-finish[k]) +1.
When calculating F, when P[i]=final, F (p,i,final) =f (p,i-1,final)
Otherwise f (P, I, Final) =f (p,i-1,6-p[i]-final) +2^ (i-1) because according to the classical Hanoi conclusion, moving the whole I plate to another pillar requires 2^i-1.
#include <cstdio>long long F (int*p,int I,int final) {if (i==0) return 0;if (p[i]==final) return F (p,i-1,final); Return f (P,i-1,6-final-p[i]) + (1ll<< (i-1));} int main () {int begin[65],finish[65],n,kase=1;while (scanf ("%d", &n) ==1&&n) {int k=0;for (int i=1;i<=n;i + +) scanf ("%d", &begin[i]), for (int i=1;i<=n;i++) scanf ("%d", &finish[i]), and for (int i=n;i>0;i--) if (begin[ I]!=finish[i]) {k=i;break;} Long Long ans= (K>0?f (Begin,k-1,6-begin[k]-finish[k]) +f (Finish,k-1,6-begin[k]-finish[k]) +1:0);p rintf ("Case%d:% Lld\n ", Kase++,ans);} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
UVA 10795 A Different Task (New Hannow tower problem)