3145 tower games, 3145 Tower

Source: Internet
Author: User

3145 tower games, 3145 Tower
3145 tower games

 

Time Limit: 1 s space limit: 32000 KB title level: Silver Question View running resultsDescription Description

The Hanoi Tower issue (also known as the Hanoi Tower issue) is a well-known issue. On the three columns A, B, and C, there are n disks of different sizes (assuming the radius is 1-n respectively). At first they are all stacked on (), your goal is to move all the plates from Tower A to Tower C within the minimum valid number of moves.

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

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

2. During the process of moving, you must ensure that the large dishes cannot be above the small ones (the small ones can be placed on the large ones, and there cannot be any other sizes under the largest ones)

 

For example, if 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

2 from B to C

1 from A to C

 

Returns a number n and obtains the moving sequence of the minimum number of steps.

Input description Input Description

An integer n

Output description Output Description

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

In the next k rows, each row contains one sentence, N from X to Y, which indicates that the N disk is moved from column X to column Y. X and Y belong 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

2 from B to C

1 from A to C

Data range and prompt Data Size & Hint

N <= 10

 1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 using namespace std; 5 int tot=0; 6 void ha1(int n,char a,char c,char b) 7 { 8     if(n==0)return; 9     ha1(n-1,a,b,c);10     tot++;11     ha1(n-1,b,c,a);12 }13 void ha2(int n,char a,char c,char b)14 {15     if(n==0)return;16     ha2(n-1,a,b,c);17     tot++;18     cout<<n<<" from"<<" "<<(char)(a-32)<<" "<<"to"<<" "<<(char)(c-32)<<" "<<endl;19     ha2(n-1,b,c,a);20 }21 int main()22 {23     int n;24     cin>>n;25     ha1(n,'a','c','b');26     cout<<tot<<endl;27     tot=0;28     ha2(n,'a','c','b');29     return 0;30 }

 

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.