-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
126 lines (110 loc) · 3.86 KB
/
main.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* Maximillian Chalitsios
* 1808500
* 8/26/2022
* Solution to Problem 1
*/
//===================================================================================================//
#include <iostream>
#include <fstream>
using std::cout;
using std::string;
//===================================================================================================//
//declare functions
//===================================================================================================//
//#1 Solves TOH problem with the adjacency rule and second sub-problem
void H_Adj(int n, string start, string aux, string dest, int &num_moves, std::ofstream& outFS);
//===================================================================================================//
//#2 Solves first sub-problem
void F(int n, string A4, string A1, string dest, string start, int &num_moves, std::ofstream& outFS);
//===================================================================================================//
//main function
int main()
{
//write results to file
std::ofstream outFS("results.txt");
//total move counter
int num_moves = 0;
//declare variable n
int n = 10;
F(n, "A4", "A1", "D", "S", num_moves, outFS);
H_Adj(n, "A4", "A1", "D", num_moves, outFS);
outFS << "\nMoves: " << num_moves << std::endl;
outFS.close();
return 0;
}
//===================================================================================================//
//define function #1
void H_Adj(int n, string start, string aux, string dest, int& num_moves, std::ofstream& outFS)
{
//base case
if (n == 1)
{
num_moves++;
outFS << "\nMove(" << num_moves << ") 1 from " << start << " to " << aux;
num_moves++;
//increment by 2 to account for A2 and A3 moves
outFS << "\nMove(" << num_moves << ") 1 from " << aux << " to " << dest;
if (aux == "A1" && dest == "A4")
{
outFS << " (A1->A2->A3->A4)";
num_moves += 2;
}
return;
}
//recursive call 1
H_Adj(n - 1, start, aux, dest, num_moves, outFS);
num_moves++;
outFS << "\nMove(" << num_moves << ") " << n << " from " << start << " to " << aux;
//recursive call 2
H_Adj(n - 1, dest, aux, start, num_moves, outFS);
num_moves++;
//increment by 2 to account for A2 and A3 moves
outFS << "\nMove(" << num_moves << ") " << n << " from " << aux << " to " << dest;
if (aux == "A1" && dest == "A4")
{
outFS << " (A1->A2->A3->A4)";
num_moves += 2;
}
//recursive call 3
H_Adj(n - 1, start, aux, dest, num_moves, outFS);
}
//===================================================================================================//
//define function #2
void F(int n, string A4, string A1, string dest, string start, int& num_moves, std::ofstream& outFS)
{
//base case
if (n == 1)
{
num_moves++;
outFS << "\nMove(" << num_moves << ") 1 from " << start << " to " << dest;
num_moves++;
outFS << "\nMove(" << num_moves << ") 1 from " << dest << " to " << A1;
num_moves++;
outFS << "\nMove(" << num_moves << ") 1 from " << A1 << " to " << A4;
if (A1 == "A1" && A4 == "A4")
{
outFS << " (A1->A2->A3->A4)";
num_moves += 2;
}
return;
}
//recursive call 1
F(n - 1, A4, A1, dest, start, num_moves, outFS);
num_moves++;
outFS << "\nMove(" << num_moves << ") " << n << " from " << start << " to " << dest;
num_moves++;
outFS << "\nMove(" << num_moves << ") " << n << " from " << dest << " to " << A1;
//move tower from A4 to D
H_Adj(n - 1, A4, A1, dest, num_moves, outFS);
num_moves++;
outFS << "\nMove(" << num_moves << ") " << n << " from " << A1 << " to " << A4;
if (A1 == "A1" && A4 == "A4")
{
outFS << " (A1->A2->A3->A4)";
num_moves += 2;
}
//move tower from D to A4
H_Adj(n - 1, dest, A1, A4, num_moves, outFS);
}
//===================================================================================================//