3145 the game of the Nottingham Tower

Source: Internet
Author: User

Title Description Description

Hanoi Tower Problem (also known as Hanoi problem), is a well-known problem. On the a,b,c three pillars, there are n different sizes of discs (assuming the radii are 1-n), and at first they are stacked on my A (), and your goal is to move all the plates from tower A to tower C within the minimum number of legally moving steps.

The rules for each step in the game are as follows:

1. Each step is allowed to move only one plate (from the top of one pillar to the top of another)

2. In the process of moving, you must ensure that the large plate cannot be on the top of the small plate (small can be placed on the large top, the maximum plate can not have any other size plate)

For the case of n=3, a valid moving sequence:

1 from A to C

2 from A to B

1 from C to B

3 from A to C

1 from B to A

2 from B to C

1 from A to C

Give a number n to find a moving sequence with a minimum number of steps

Enter a description Input Description

An integer n

Output description Output Description

The first line is an integer k, which represents the minimum number of moving steps.

The next K-line, one sentence per line, n from x to Y, indicates that the N-number plate is moved from the X-column to the Y-column. X, Y belongs to {a,b,c}

Sample input Sample Input

3

Sample output Sample Output

7

1 from A to C

2 from A to B

1 from C to B

3 from A to C

1 from B to A

2 from B to C

1 from A to C

Data range and Tips Data Size & Hint

n<=10

Title Description Description

Hanoi Tower Problem (also known as Hanoi problem), is a well-known problem. On the a,b,c three pillars, there are n different sizes of discs (assuming the radii are 1-n), and at first they are stacked on my A (), and your goal is to move all the plates from tower A to tower C within the minimum number of legally moving steps.

The rules for each step in the game are as follows:

1. Each step is allowed to move only one plate (from the top of one pillar to the top of another)

2. In the process of moving, you must ensure that the large plate cannot be on the top of the small plate (small can be placed on the large top, the maximum plate can not have any other size plate)

For the case of n=3, a valid moving sequence:

1 from A to C

2 from A to B

1 from C to B

3 from A to C

1 from B to A

2 from B to C

1 from A to C

Give a number n to find a moving sequence with a minimum number of steps

Enter a description Input Description

An integer n

Output description Output Description

The first line is an integer k, which represents the minimum number of moving steps.

The next K-line, one sentence per line, n from x to Y, indicates that the N-number plate is moved from the X-column to the Y-column. X, Y belongs to {a,b,c}

Sample input Sample Input

3

Sample output Sample Output

7

1 from A to C

2 from A to B

1 from C to B

3 from A to C

1 from B to A

2 from B to C

1 from A to C

Data range and Tips Data Size & Hint

n<=10

#include <cstdio>#include<iostream>using namespacestd;voidHanoiintKCharACharBCharc) {    if(k==1) cout<<k<<" from"<<a<<" to"<<c<<Endl; Else{Hanoi (k-1, a,c,b); cout<<k<<" from"<<a<<" to"<<c<<Endl; Hanoi (K-1, B,a,c); }}intMain () {intn,ans[101]={0}; CIN>>N;  for(intI=1; i<=n;i++) Ans[i]=2*ans[i-1]+1; cout<<ans[n]<<Endl; Hanoi (N,'A','B','C');}

3145 the game of the Nottingham Tower

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.