Use c ++ to move a plate

Source: Internet
Author: User

There is a stack of dishes on the table. There are only three stacks of dishes on the table at the same time. small dishes can only be placed on large ones. In order not to break the plate, only one can be moved at a time, the plate moves from the left to the right as quickly as possible. Is it possible to move the disk in some situations?

 

 

 

 

 

 

 

 

 

Moving n dishes from the left to the right is equivalent
N-1-1 plates move from the left to the middle
The Nth plate moves fastest from the left to the right
N-1-1 dishes move from the middle to the right

 

 

 

 

 

 

 

 

 

When n dishes move from the left fastest to the right, is it possible that some situations are equivalent
Is the first n-th plate on the left possible when the first n-th plate moves from the left to the middle as soon as possible?
Is the first n-n plates on the right likely to appear when the first n-n plates move from the middle to the right as soon as possible?

Quantize first


 
1

3 2

5 4

8 7 6


Run
How many dishes do you have?
8
How many dishes in the left?
4
Whats the sequence of the dishes?
8 5 3 1
How many dishes in the middle?
3
Whats the sequence of the dishes?
7 4 2
How many dishes in the right?
1
Whats the sequence of the dishes?
6
Wait a minute.
What a pity!

 

// Copyright (c) LeafCore
# Include <iostream>
Using namespace std;

// Number of dishes
Const int const_m = 64;
// Order of storing dishes
Int dish [3] [const_m];
// Point to the Bottom Plate
Int bottom [3];

// Is it possible to move n plates from?
Bool possible (int n, int from, int)
{
// Recursive exit. Is it possible to move only one plate from?
If (n = 1 ){
If (dish [from] [bottom [from] = 1 | dish [to] [bottom [to] = 1 ){
Return true;
} Else {
Return false;
}
}
// The largest plate is on the left.
If (dish [from] [bottom [from] = n ){
// Skip the largest plate
Bottom [from] ++;
// Whether the first n-1-1 plates may appear when they are moved from one to another.
Return possible (n-1, from, 3-from-);
}
// The largest plate is on the right.
If (dish [to] [bottom [to] = n ){
// Skip the largest plate
Bottom [to] ++;
// Is it possible to move the first n-1 plates from another position other than from and?
Return possible (n-1, 3-from-to, );
}
Return false;
}

Int main ()
{
Int m, n;
While (true ){
// Enter the number of dishes
Cout <"How many dishes do you have? "<Endl;
Cin> n;
For (int I = 0; I <3; I ++ ){
// Enter the number of dishes at each position
Cout <"How many dishes in the" <(I = 0? "Left" :( I = 1? "Middle": "right") <"? "<Endl;
Cin> m;
// Input sequence of Plates
Cout <"Whats the sequence of the dishes? "<Endl;
For (int j = 0; j <m; j ++ ){
Cin> dish [I] [j];
}
// Point to the Bottom Plate
Bottom [I] = 0;
}
Cout <"Wait a minute." <endl;
// Output result
Cout <(possible (n, 0, 2 )? "Awesome! ":" What a pity! ") <Endl;
Getchar ();
Getchar ();
}
Return 0;
}

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.