Tower of Hanoi
Tower II hdu1207:
First, let's talk about hanruita I (the classic hanruita problem). There are three towers. from small to large, tower a has n dishes from top to bottom, and now it's about to move to target c,
A smaller rule must be placed on a larger one. Move one at a time and calculate the minimum number of steps. This problem is simple. DP: A [n] = A [n-1] + 1 + a [n-1] First
N-1 put on B, put the largest on the target C, and then put the N-1 back to C.
An Optimal Solution on the internet is as follows: (1) moving X (1 <= x <= N) disks from column A to column C by Column B and column D, the number of steps required in this process is f [X]; (2) Move the n-x disks on column A to column D by Column B (Note: C Columns cannot be used at this time, because all disks on column C are smaller than those on column A. The moving mode is equivalent to a classic Tower, that is, the number of steps required for this process is 2 ^ (n-x) -1 (for proof, refer to Tower 1 again); (3) Move x disks on column C to column A and column B to column D. the steps required for this process are f [X]. the task is completed after step (3. Therefore, the total number of steps required for task completion f [N] = f [x] + 2 ^ (n-x) -1 + F [x] = 2 * f [x] + 2 ^ (n-x)-1; but this has not yet met the requirements, the question requires the minimum number of steps. It is easy to understand. With the different values of X, for the same N, different f [N] will be obtained. That is, the actual answer to this question should be min {2 * f [x] + 2 ^ (n-x)-1}, where 1 <= x <= N; in the process of implementing this algorithm using advanced languages, we can traverse the values of X in a circular manner, use the variable min to record the minimum value of F [N] in each value of X.
# Include "stdio. H "# include" string. H "# include" math. H "# define n 66 # define INF 0x7fffffint main () {_ int64 I, j, Min, F [N] = {0, 1, 3 };; for (I = 3; I <n; I ++) {min = inf; For (j = 1; j <I; j ++) {If (min> 2 * f [J] + POW (2.0, 1.0 * I-j)-1) // the return value of POW exceeds 64 bits, cannot be forcibly converted to integer min = 2 * f [J] + POW (2.0, 1.0 * I-j)-1; // note that both parameters should be double type !! } F [I] = min;} while (scanf ("% i64d", & I )! =-1) {printf ("% i64d \ n", F [I]);} return 0 ;}
Hdu2064:
First move the above N-1 to C (must have this State), move the largest to B, then move the N-1 to A, move the largest to C, then move the N-1 to C.
Recursive Formula: F [N] = f [n-1] + 1 + F [n-1] + 1 + F [n-1]; that is, F [N] = 3 * f [n-1] + 2;
#include"stdio.h"#include"string.h"#include"math.h"#define N 36int main(){ __int64 n,i,f[N]={2}; for(i=1;i<N;i++) { f[i]=3*f[i-1]+2; } while(scanf("%I64d",&n)!=-1) { printf("%I64d\n",f[n-1]); } return 0;}
Hanruata iv hdu 2077
On the basis of hanruita 3, modify the condition: allow the largest plate to be placed on the top (only allow the largest to put on the top) of course, the final result is that the dishes are arranged from small to large on the rightmost.
Three Towers A, B, and C. equation: ANS [N] = AB [n-1] + 1 + 1 + bc [n-1]. (AB indicates A to B)
DP idea: first move n-1 to B, then use the two steps as largest to C, and then the n-1 from B to C. Here we need to obtain the recursive equations of AC [N] and BC [N]: BC [N] = BC [n-1] + 1 + AC [n-1], (1 type)
The BC [N] equation is the same as AB [N. So the total equation ans [N] = 2 * BC [n-1] + 2. (2)
#include"stdio.h"#include"string.h"#include"math.h"#define N 21int main(){ int i,T; __int64 ac[N],bc[N],ans[N]; ac[1]=2; bc[1]=1; for(i=2;i<N;i++) { ac[i]=3*ac[i-1]+2; bc[i]=bc[i-1]+ac[i-1]+1; ans[i]=2*bc[i-1]+2; } ans[1]=2; scanf("%d",&T); while(T--) { scanf("%d",&i); printf("%I64d\n",ans[i]); } return 0;}
Hdu1995
Attach a question to the classic hanruata question: Find the number of times the plate K is moved when n plates are created.
The concept is two-dimensional DP, DP [N] [I] = DP [n-1] [I] * 2 (1 = <I <n ), DP [N] [N] = 1;
The maximum tray is moved only once. The above plate is first moved to Tower B, once, and finally from B to target c again ..
#include"stdio.h"#include"string.h"#include"math.h"#define N 61int main(){ __int64 i,j,f[N][N]; f[1][1]=f[2][2]=1; f[2][1]=2; for(i=3;i<N;i++) { f[i][i]=1; for(j=1;j<i;j++) { f[i][j]=2*f[i-1][j]; } } int T,n,m; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); printf("%I64d\n",f[n][m]); } return 0;}
Hdu1996
Each disk has three options from small to large, with a total of 3 ^ n.
#include"stdio.h"#include"string.h"#include"math.h"#define N 61int main(){ int T,n; scanf("%d",&T); while(T--) { scanf("%d",&n); printf("%.0f\n",pow(3.0,n*1.0)); } return 0;}