-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhanoi.cpp
103 lines (90 loc) · 2.21 KB
/
hanoi.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
#include <iostream>
#include <cstdlib>
using namespace std;
struct List
{
int disk;
List *next;
};
int count = 0;
void show(List **disks)
{
List *currents[3];
for (int i = 0; i < 3; i++)
currents[i] = disks[i];
while (currents[0] || currents[1] || currents[2])
{
for (int i = 0; i < 3; i++)
{
if (currents[i] != NULL)
cout << currents[i]->disk << " ";
else
cout << "-" << " ";
}
for (int i = 0; i < 3; i++)
if (currents[i])
currents[i] = currents[i]->next;
cout << endl;
}
}
void throw_disk(int from, int to, List **disks)
{
List *tempfrom = disks[from];
disks[from] = disks[from]->next;
tempfrom->next = disks[to];
disks[to] = tempfrom;
}
hanoi_towers(int quantity, int from, int to, int buf_peg, List **disks) //quantity-????? ?????, from-????????? ????????? ?????(1-3),to-???????? ????????? ?????(1-3)
{ //buf_peg - ????????????? ???????(1-3)
if (quantity != 0)
{
count++;
hanoi_towers(quantity-1, from, buf_peg, to, disks);
show(disks);
cout << from << " -> " << to << endl;
throw_disk(from, to, disks);
hanoi_towers(quantity-1, buf_peg, to, from, disks);
}
}
List *get_new_list(int plate, List **disks)
{
List *disk = (List*)malloc(sizeof(List));
disk->disk = plate;
disk->next = *disks;
*disks = disk;
}
List **get_towers(int plate)
{
List *current;
List **disks = (List**)malloc(sizeof(List*) * 3);
disks[0] = (List*)malloc(sizeof(List));
disks[0]->disk = plate;
disks[0]->next = NULL;
cout << "smth" << endl;
current = disks[0];
for(int i = plate - 1; i > 0; i--)
get_new_list(i, &(disks[0]));
disks[1] = NULL;
disks[2] = NULL;
return (disks);
}
void freemem(List *disks)
{
if (disks)
freemem(disks->next);
free(disks);
}
int main()
{
int start_peg, destination_peg, buffer_peg, plate_quantity;
List **disks;
cout << "count" << endl;
cin >> plate_quantity;
disks = get_towers(plate_quantity);
hanoi_towers(plate_quantity, 0, 2, 1, disks);
show(disks);
cout << count << endl;
freemem(disks[2]);
free(disks);
return 0;
}