Explanation of hdu tower Problems

Source: Internet
Author: User

There are two game modes for the tower of Hanoi Problem: the first is the existence of 1, 2, 3 pillars, each time can only move the plate to the adjacent location, each can only move one plate. (If you move the plate on column 1 directly to column 3 once, for example, there are n plates, the minimum step for moving all of Column 1 to column 3 is the step required to move all of the plates (n and n) to the adjacent positions. Adjacent plates refer to steps from 1-> 2. B to move all the plates (n and n above) to non-adjacent locations. Adjacent plates refer to from 1-> 3. a B
1 1 2 a [1] B [1]
2 4 8 B [1] + 1 + a [1] B [1] + 1 + B [1] + 1 + B [1]
3 13 26 B [2] + 1 + a [2] B [2] + 1 + B [2] + 1 + B [2]
4 40 80 B [3] + 1 + a [3] B [3] + 1 + B [3] + 1 + B [3]
N B [n-1] + a [n-1] + 1 B [n-1] + 1 + B [n-1] + 1 + B [n-1]
N B [n-1] + a [n-1] + 1 3 * B [n-1] + 2
For n plates on one pillar, the minimum steps to move them to the three pillars are, B [n-1] + 1 + B [n-1] + 1 + B [n-1] Step 1 is to regard the (n-1 plate and The N-2 plate above) on column 1 as overall) move to column 3. The number of moving steps is B [n-1]. The second step is to move the plate n on the first part to the column 2, and the number of moving steps is 1 step. Step 3 is to move column 3 (n-1 plate and The N-2 plate above as a whole) to column 1. The number of moving steps is B [n-1] Step 4 is to move the nth plate on the 2nd to the 3th column, and the number of moving steps is 1 step. Step 5 is to move column 1 (n-1 plate and The N-2 plate above as a whole) to column 3. The number of steps is B [n-1] to complete the whole process. The first one is that there are 1, 2, and 3 columns. Each time you can move the plate to any position, you can only move one plate at a time. (If you move the plate on column 1 to column 3 once, for example, there are n plates, the minimum steps for moving all the data from pillar 1 to Pillar 3 are 2 ^ n-1. Second game mode, tower V

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 1301 Accepted Submission (s): 811


Problem Description uses 1, 2,..., and n to represent n dishes. It is called Disk 1, Disk 2, and ,.... The number of plates is big. Classic Tower of Hanoi
The question is often used as a typical recursive example. Some people may not know the story of the tower. Tower of Hanoi
A story from the legend of India, where God made three diamond pillars in the world and pressed them up and down
64 gold disks stacked in sequence. God commands Brahman to place the disc from below in order of size and then in another column
. It is also stipulated that the disc cannot be enlarged on a small disc, and only one disc can be moved between the three pillars. We
We know that we need to move at least 2 ^ 64-1 times. During the moving process, we found that some disks are moving more often, and some are moving less. Notice Board
Calculates the number of times the plate is moved.


Input contains multiple groups of data. Input T indicates that there are T groups of data. Each Data row contains N (1 <= N <= 60) and disks.
Number k (1 <= k <= N ).


Output outputs a number for each group of data. The minimum number of moves required for disk k when the target is reached.


Sample Input

260 13 1
 


Sample Output

5764607523034234884
The post code understands the problem process and finds the rule to solve it easily.

#include<stdio.h>__int64 a[22];int main(){    a[0]=0;    a[1]=2;    int i,n;    for(i=2;i<=21;i++)      a[i]=3*a[i-1]+2;    scanf("%d",&n);    while(n--)        {       scanf("%d",&i);       printf("%I64d\n",a[i-1]+2);                                }        }
 
The first game mode

Tower V

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 1302 Accepted Submission (s): 812


Problem Description uses 1, 2,..., and n to represent n dishes. It is called Disk 1, Disk 2, and ,.... The number of plates is big. Classic Tower of Hanoi
The question is often used as a typical recursive example. Some people may not know the story of the tower. Tower of Hanoi
A story from the legend of India, where God made three diamond pillars in the world and pressed them up and down
64 gold disks stacked in sequence. God commands Brahman to place the disc from below in order of size and then in another column
. It is also stipulated that the disc cannot be enlarged on a small disc, and only one disc can be moved between the three pillars. We
We know that we need to move at least 2 ^ 64-1 times. During the moving process, we found that some disks are moving more often, and some are moving less. Notice Board
Calculates the number of times the plate is moved.


Input contains multiple groups of data. Input T indicates that there are T groups of data. Each Data row contains N (1 <= N <= 60) and disks.
Number k (1 <= k <= N ).


Output outputs a number for each group of data. The minimum number of moves required for disk k when the target is reached.


Sample Input

260 13 1
 


Sample Output

5764607523034234884
 

Post code:

#include<stdio.h>__int64 a[22];int main(){    a[0]=0;    a[1]=2;    int i,n;    for(i=2;i<=21;i++)      a[i]=3*a[i-1]+2;    scanf("%d",&n);    while(n--)        {       scanf("%d",&i);       printf("%I64d\n",a[i-1]+2);                                }        }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.