-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
61 lines (43 loc) · 2.64 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
#include "minMoves.h"
int main() {
int startPosition[2]; // initial coordinates of the knight
int finalPosition[2]; // coordinates of the field where the knight should come
PrintTable();
cout << "\nEnter the x-coordinate of the knight's starting field: "; cin >> startPosition[0];
cout << "Enter the y-coordinate of the knight's starting field: "; cin >> startPosition[1];
if( !isInTable(startPosition[0],startPosition[1]) ) { // We check whether the initial coordinates entered are valid
cout << "You have entered invalid coordinates!!!";
exit(0);
}
cout << "\nEnter the x-coordinate of the knight's final field: "; cin >> finalPosition[0];
cout << "Enter the y-coordinate of the knight's final field:"; cin >> finalPosition[1];
if( !isInTable(finalPosition[0],finalPosition[1]) ) { // We check whether the final coordinates entered are valid
cout << "You have entered invalid coordinates!!!";
exit(0);
}
Field* minimum = FindMinimum(startPosition,finalPosition); // We search for the shortest path and return the address of the last (requested) field
system("cls");
cout << "The minimum number of moves is: " << minimum->distance << endl;
vector<Field*> moves;
while(minimum->previous != NULL) { // Since each Field contains the address of the previous field,
moves.push_back(minimum); // we place the fields from the last (end) into the vector
minimum = minimum->previous; // towards the first (initial) and we get the knight's movement path in the moves vector
}
// We print the entire path of the knight (from the starting field to the final one)
cout << "\nPath: " << endl;
cout << "[" << startPosition[0] << "," << startPosition[1] << "]" << endl; // Print the coordinates of the first (initial) field
while(!moves.empty()) { // we print the coordinates of all fields that are in vector<Field*>moves starting from the last one
cout << "[" << moves.back()->xCoordinate << "," << moves.back()->yCoordinate << "]" << endl;
markField(moves.back()->xCoordinate,moves.back()->yCoordinate,'X'); // we mark each Field over which the knight crossed with an 'X'
moves.pop_back();
}
moves.clear();
markField(startPosition[0],startPosition[1],'S');
markField(finalPosition[0],finalPosition[1],'F');
cout << "\n";
PrintTable();
cout << "\nS - starting position of the knight" << endl;
cout << "F - final position of the knight" << endl;
cout << "X - fields over which the knight reached the final position" << endl;
return 0;
}