-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathknight.cpp
More file actions
63 lines (57 loc) · 1.73 KB
/
knight.cpp
File metadata and controls
63 lines (57 loc) · 1.73 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
#include <array>
#include <algorithm>
#include <iostream>
using cell = std::pair<int, int>;
static const int move_x[8] = {+2, +2, +1, -1, -2, -2, +1, -1};
static const int move_y[8] = {+1, -1, +2, +2, +1, -1, -2, -2};
static bool board[8][8];
std::array<cell, 64> path;
bool valid_cell(cell c) {
if ((c.first < 1 || c.second < 1) || (c.first > 8 || c.second > 8))
return false;
return true;
}
int count_available_neighbors(cell c) {
if (!valid_cell(c) || board[c.first][c.second])
return 0;
int count = 0;
for (int i = 0; i < 8; ++i) {
int x = c.first + move_x[i];
int y = c.second + move_y[i];
if (valid_cell({x, y}) && !board[x][y])
++count;
}
return count;
}
void backtrack(cell c, int d) {
if (!d) {
std::cerr << "FOUND!\n";
for (auto c: path)
std::cerr << "(" << c.first << ", " << c.second << "), ";
std::cerr << '\n';
}
std::array<std::pair<int, cell>, 8> neighbor;
for (int i = 0; i < 8; ++i) {
int x = c.first + move_x[i];
int y = c.second + move_y[i];
neighbor[i] = {count_available_neighbors({x, y}), {x, y}};
}
std::sort(begin(neighbor), end(neighbor));
for (auto e: neighbor) {
if (valid_cell(e.second) && !board[e.second.first][e.second.second]) {
int x = e.second.first;
int y = e.second.second;
board[x][y] = true;
path[d - 1] = {x, y};
backtrack({x, y}, d - 1);
board[x][y] = false;
path[d - 1] = {0, 0};
}
}
}
int main() {
path[63] = {1, 1};
board[1][1] = true;
backtrack({1, 1}, 63);
return 0;
}