-
Notifications
You must be signed in to change notification settings - Fork 2
/
10017.cpp
executable file
·99 lines (85 loc) · 1.6 KB
/
10017.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std ;
long long int n = 0 ;
long long int counter = 0 ;
vector<int> disk[ 3 ] ;
void printState()
{
int i = 0 ; // loop counter
cout << endl ;
cout << "A=>" ;
for( i = 0 ; i < disk[ 0 ].size() ; i++ )
{
if( i == 0 )
cout << " " << disk[ 0 ][ i ] ;
else
cout << " " << disk[ 0 ][ i ] ;
}
cout << endl ;
cout << "B=>" ;
for( i = 0 ; i < disk[ 1 ].size() ; i++ )
{
if( i == 0 )
cout << " " << disk[ 1 ][ i ] ;
else
cout << " " << disk[ 1 ][ i ] ;
}
cout << endl ;
cout << "C=>" ;
for( i = 0 ; i < disk[ 2 ].size() ; i++ )
{
if( i == 0 )
cout << " " << disk[ 2 ][ i ] ;
else
cout << " " << disk[ 2 ][ i ] ;
}
cout << endl ;
}
void hanoi( int movingPeg, int from, int to )
{
int i = 0 ; // loop counter
if( movingPeg > 1 )
{
hanoi( movingPeg - 1, from, 3 - from - to ) ;
}
if( counter < n )
{
counter++ ;
disk[ to ].push_back( disk[ from ].back() ) ;
disk[ from ].pop_back() ;
printState() ;
}
else
{
return ;
}
if( movingPeg > 1 )
{
hanoi( movingPeg - 1, 3 - from - to, to ) ;
}
}
int main()
{
int m = 0 ;
int problemSet = 0 ;
int i = 0 ; // loop counter
while( cin >> m >> n )
{
if( m == 0 && n == 0 )
break ;
problemSet++ ;
disk[ 0 ].resize( m, 0 ) ;
for( i = 0 ; i < m ; i++ )
disk[ 0 ][ i ] = m - i ;
disk[ 1 ].clear() ;
disk[ 2 ].clear() ;
cout << "Problem #" << problemSet << endl ;
counter = 0 ;
printState() ;
hanoi( m, 0, 2 ) ;
cout << endl ; // Print a blank line after every problem.
}
return 0 ;
}