Strange Towers of Hanoi
Time Limit: 1000MS |
|
Memory Limit: 30000K |
Total Submissions: 2678 |
|
Accepted: 1742 |
Description
Background
Charlie Darkbrown sits in another one of those boring computer science lessons:at the moment the teacher just explains th E standard Tower of Hanoi problem, which bores Charlie to death!
The teacher points to the blackboard (Fig. 4) and says: "So here is the problem:
- There is three towers:a, B and C.
- There is n disks. The number n is constant while working the puzzle.
- All disks is different in size.
- The disks is initially stacked on tower A increasing in size from the top to the bottom.
- The goal of the puzzle is to transfer all of the disks from tower A to Tower C.
- One disk at a time can is moved from the top of a tower either to an empty tower or to a tower with a larger disk on the T Op.
So your task was to write a program this calculates the smallest number of disk moves necessary to move all the disks from Tower A to C. "
Charlie: "This is incredibly boring-everybody knows," This can be solved using a simple recursion. I deny to code something as simple as this! "
The teacher sighs: "Well, Charlie, let's think about something-to-do:for you there are a fourth tower D. Calculate The smallest number of disk moves to move all the disks from the tower A to the tower D using any four towers. "
Charlie looks irritated: "Urgh ... Well, I don ' t know a optimal algorithm for four towers ... "
problem
So the real problem was that problem solving does not belong to the things Charlie was good at. Actually, the only thing Charlie are really good at are "sitting next to someone who can do the job". And now Guess what-exactly! It is the sitting next to Charlie, and he's already glaring at you.
Luckily, you know this following algorithm works for n <= 12:at first k >= 1 disks on Tower A is fixed and the Remaining n-k disks is moved from tower A to tower B using the the algorithm for four towers. Then the remaining K disks from Tower A is moved to tower D using the the algorithm for three towers. At last the N-k disks from Tower B is moved to tower D again using the algorithm for four towers (and thereby not Movin G any of the K disks already on Tower D). Do the to all K 2∈{1, ...., n} and find the K with the minimal number of moves.
So for n = 3 and k = 2 would first move 1 (3-2) disk from Tower A to tower B using the the algorithm for four towers (one Move). Then you would move the remaining of the disks from tower A to tower D using the algorithm for three towers (three moves). And the last step would is to move the disk from Tower B to tower D using again the algorithm for four towers (another MOV e). Thus the solution for n = 3 and k = 2 is 5 moves. To being sure that this really are the best solution for n = 3 You need to check the other possible values 1 and 3 for K. , by the the-the-optimal, 5 is ...)
Input
There is no input.
Output
For each n (1 <= n <=) print a single line containing the minimum number of moves to solve the problem for four T Owers and N disks.
Sample Input
No input.
Sample Output
REFER to OUTPUT.
Hanoi tower problem, relatively simple, but it should be noted that the topic is not entered.
Test instructions: 4 pillars Hanoi, no input, direct output of the minimum number of steps of 1-12 plates respectively
Attached code:
1#include <iostream>2#include <cstdio>3 using namespacestd;4 intMinintAintb)5 {6 returnA>b?b:a;7 }8 intMain ()9 {Ten intn,m,i,j; One inta[ the]= {0,1},dp[ the]; A for(i=2; i<= A; i++) -a[i]=a[i-1]*2+1;//the number of steps required to move a plate when three poles -dp[0]=0; the for(i=1; i<= A; i++) - { -dp[i]=100000; - for(j=1; j<=i; J + +) +Dp[i]=min (Dp[i],2*dp[i-j]+A[j]); - //Move the i-j plate to the B pillar and move the remaining J plate to the C pillar. and move that i-j plate. + } A for(i=1; i<= A; i++) atprintf"%d\n", Dp[i]); - return 0; -}
POJ 1958 Strange Towers of Hanoi