-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnightsTour.cpp
More file actions
61 lines (50 loc) · 1.57 KB
/
KnightsTour.cpp
File metadata and controls
61 lines (50 loc) · 1.57 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
#include <iostream>
#include <vector>
#define endl "\n"
using namespace std;
bool KnightsTour(int**visited, int row, int col, vector<int> pathRow, vector<int> pathCol, int move);
bool valid(int** visited, int nrow, int ncol);
int main(void) {
vector<int> pathRow = {-2, -2, -1, -1, 1, 1, 2, 2};
vector<int> pathCol = {-1, 1, -2, 2, -2, 2, -1, 1};
int** visited = new int*[8];
for(int i = 0; i < 8; i++) {
visited[i] = new int[8];
}
visited[0][0] = 1;
KnightsTour(visited, 0, 0, pathRow, pathCol, 1);
return 0;
}
bool KnightsTour(int** visited, int row, int col, vector<int> pathRow, vector<int> pathCol, int move) {
if(move == 64) { // 8x8 chessboard
for(int i = 0; i < 8; i++) {
for(int j = 0; j < 8; j++) {
cout << visited[i][j] << " ";
}
cout << endl;
}
return true;
}
else {
for(int i = 0; i < pathRow.size(); i++) {
int nrow = row + pathRow[i];
int ncol = col + pathCol[i];
if(valid(visited, nrow, ncol)) {
move++;
visited[nrow][ncol] = move;
if(KnightsTour(visited, nrow, ncol, pathRow, pathCol, move)) {
return true;
}
move--;
visited[nrow][ncol] = 0;
}
}
}
return false;
}
bool valid(int** visited, int nrow, int ncol) {
if((nrow >= 0) && (nrow < 8) && (ncol >= 0) && (ncol < 8) && (visited[nrow][ncol] == 0)) {
return true;
}
return false;
}