forked from USPCodeLabSanca/dev.hire-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnightsTour.cpp
More file actions
115 lines (84 loc) · 2.69 KB
/
Copy pathKnightsTour.cpp
File metadata and controls
115 lines (84 loc) · 2.69 KB
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
// Will prob have some erros, but could not test it on hackerank
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int knightI[8] = { 2, 1, -1, -2, -2, -1, 1, 2 };
int knightJ[8] = { 1, 2, 2, 1, -1, -2, -2, -1 };
struct {
unordered_map<pair<int, int>, bool> visitedPlaces;
int positionsToVisit;
int n;
int m;
} TestCase;
void blockCell(TestCase& tCase, int i, int j) {
// Check if is a valid cell position
if (!(i >= 0 && i < tCase->m && j >= 0 && j < tCase->n))
return;
// Block cell
pair<int, int> position = make_pair(i, j);
tCase->visitedPlaces[position] = true;
tCase->positionsToVisit--;
}
bool checkIfCellIsBlocked(TestCase& case, int i, int j) {
// Check if is a valid cell position
if (!(i >= 0 && i < tCase->m && j >= 0 && j < tCase->n))
return false;
// Get cell
pair<int, int> position = make_pair(i, j);
if (tCase->visitedPlaces.find(position) == tCase->visitedPlaces.end())
return false;
return true;
}
vector<pair<int, int>> doTestCase(TestCase tCase, int i, int j) {
vector<pair<int, int>> visited;
// Check if cell is alredy blocked or is an invalid position
if (checkIfCellIsBlocked(tCase, i, j))
return visited;
// If not block cell
blockCell(&tCase, i, j);
// Check if alredy visited every place
if (tCase.positionsToVisit == 0) {
visited.push_back(make_pair(i, j));
} else {
// Continue the tour
for (int n = 0; n < 8; n++) {
vector<pair<int, int>> childVisited;
childVisited = doTestCase(tCase, i + knightI[n], j + knightJ[n]);
// Check if found solution
if (childVisited.size() > 0) {
visited.push_back(make_pair(i, j));
break;
}
}
}
return visited;
}
int main() {
TestCase tCase;
// Read the size of the chess table
cin >> tCase.m >> tCase.n;
tCase.positionsToVisit = tCase.m * tCase.n;
// Number of blocked cells
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int x;
int y;
cin >> x >> y;
blockCell(&tCase, x, y);
}
// Do testCase
vector<pair<int, int>> visitedPlaces = doTestCase(tCase, i, j);
if (visitedPlaces.length() == 0) {
cout << "No";
} else {
for (int i = visitedPlaces.size() - 1; i >= 0; i--) {
pair<int, int> position = visitedPlaces[i];
cout << pair.first << pair.second << endl;
}
}
return 0;
}