Output process of the wiki 3145 Tower

Source: Internet
Author: User

Description 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


<span style="font-size:18px;">#pragma comment(linker, "/STACK:1024000000,1024000000")#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<map>#include<queue>#include<set>#include<cmath>#include<bitset>#define mem(a,b) memset(a,b,sizeof(a))#define lson i<<1,l,mid#define rson i<<1|1,mid+1,r#define llson j<<1,l,mid#define rrson j<<1|1,mid+1,r#define INF 1000000007#define seed 13131#define seed1 1313#define maxn 20typedef long long ll;typedef unsigned long long ull;using namespace std;#define step = 0void Move(char from, char to, char help, int  num){    printf("%d from %c to %c\n",num,from,to,help);}void Hanoi(char from,char to,char help,int  num){    if(num==1) {Move(from,to,help,1);return;}    Hanoi(from,help,to,num-1);    Move(from,to,help,num);    Hanoi(help,to,from,num-1);}main(){    int num;    scanf("%d",&num);    printf("%d\n",(1<<num)-1);    Hanoi('A','C','B',num);    return 0;}</span>


Output process of the wiki 3145 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.