-
Notifications
You must be signed in to change notification settings - Fork 0
/
633 - A Chess Knight.cpp
204 lines (161 loc) · 4.61 KB
/
633 - A Chess Knight.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
typedef enum {NONE, K, B, T} MoveType;
class Coord;
class Move;
class Game;
class Coord {
public:
Coord(int x, int y) {
this->x = x;
this->y = y;
}
int x, y;
bool operator==(const Coord &rhs) const {
return x == rhs.x && y == rhs.y;
}
bool operator!=(const Coord &rhs) const {
return !(rhs == *this);
}
vector<Move> moves(int size);
};
class Move {
public:
Move(Coord coord, MoveType type) : coord(coord), type(type) {
}
Coord coord;
MoveType type;
};
vector<Move> Coord::moves(int size) {
vector<Move> all = {
// K moves
Move(Coord(x - 2, y - 1), K),
Move(Coord(x - 2, y + 1), K),
Move(Coord(x - 1, y - 2), K),
Move(Coord(x + 1, y - 2), K),
Move(Coord(x + 2, y - 1), K),
Move(Coord(x + 2, y + 1), K),
Move(Coord(x - 1, y + 2), K),
// B moves
Move(Coord(x + 1, y + 2), B),
Move(Coord(x - 2, y - 2), B),
Move(Coord(x - 2, y + 2), B),
Move(Coord(x + 2, y - 2), B),
Move(Coord(x + 2, y + 2), B),
// T moves
Move(Coord(x, y >= size / 2 ?
size / 2 - (y - size / 2) - 1 :
size / 2 + (size / 2 - y) - 1), T),
Move(Coord(x >= size / 2 ?
size / 2 - (x - size / 2) - 1 :
size / 2 + (size / 2 - x) - 1,
y), T)
};
vector<Move> result;
for (Move move : all) {
if (move.coord.x >= 0 && move.coord.y >= 0 && move.coord.x < size && move.coord.y < size) {
result.push_back(move);
}
}
return result;
}
class Game {
public:
explicit Game(int n) {
size = 2 * n;
map = new bool**[size];
for (int i = 0; i < size; i++) {
map[i] = new bool*[size];
for (int j = 0; j < size; j++) {
map[i][j] = new bool[4];
for (int k = 0; k < 4; k++) {
map[i][j][k] = false;
}
}
}
}
~Game() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
delete[] map[i][j];
}
delete[] map[i];
}
delete[] map;
}
void addObstacle(const Coord &coord) {
map[coord.x][coord.y][0] = true;
}
bool isObstacle(const Coord &coord) {
return map[coord.x][coord.y][0];
}
void visit(const Coord &coord, MoveType moveType) {
if (moveType == K) {
map[coord.x][coord.y][1] = true;
} else if (moveType == B) {
map[coord.x][coord.y][2] = true;
} else if (moveType == T) {
map[coord.x][coord.y][3] = true;
}
}
bool isAlreadyVisited(const Coord &coord, MoveType moveType) {
if (moveType == K) {
return map[coord.x][coord.y][1];
} else if (moveType == B) {
return map[coord.x][coord.y][2];
} else if (moveType == T) {
return map[coord.x][coord.y][3];
}
return false;
}
int size;
bool ***map;
};
int main() {
int n;
cin >> n;
while (n != 0) {
Game game(n);
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
Coord start(x1 - 1, y1 - 1);
Coord end(x2 - 1, y2 - 1);
int xO, yO;
cin >> xO >> yO;
while (xO != 0 && yO != 0) {
game.addObstacle({xO - 1, yO - 1});
cin >> xO >> yO;
}
int count = 0;
queue<Move> q, next;
q.push({start, NONE});
bool found = false;
while (!q.empty() && !found) {
while (!q.empty() && q.front().coord != end) {
Move last = q.front();
q.pop();
for (Move &move : last.coord.moves(game.size)) {
if (move.type != last.type && !game.isObstacle(move.coord) && !game.isAlreadyVisited(move.coord, move.type)) {
next.push(move);
game.visit(move.coord, move.type);
}
}
}
if (q.empty()) {
count++;
swap(q, next);
} else if (q.front().coord == end) {
found = true;
}
}
if (q.empty()) {
cout << "Solution doesn't exist" << endl;
} else {
cout << "Result : " << count << endl;
}
cin >> n;
}
return 0;
}