forked from MAYANK25402/Hactober-2023-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KnightTourProblem.cpp
56 lines (49 loc) · 1.34 KB
/
KnightTourProblem.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
#include<iostream>
#include<vector>
using namespace std;
const int N = 8;
int board[N][N];
int moveCount = 0;
vector<pair<int, int>> validMoves(int row, int col) {
vector<pair<int, int>> moves;
int rowMoves[] = {-2, -1, 1, 2, 2, 1, -1, -2};
int colMoves[] = {1, 2, 2, 1, -1, -2, -2, -1};
for (int i = 0; i < 8; i++) {
int newRow = row + rowMoves[i];
int newCol = col + colMoves[i];
if (newRow >= 0 && newRow < N && newCol >= 0 && newCol < N && board[newRow][newCol] == 0) {
moves.push_back(make_pair(newRow, newCol));
}
}
return moves;
}
bool solve(int row, int col, int moveNum) {
board[row][col] = moveNum;
moveCount++;
if (moveNum == N*N) {
return true;
}
vector<pair<int, int>> moves = validMoves(row, col);
for (pair<int, int> move : moves) {
int newRow = move.first;
int newCol = move.second;
if (solve(newRow, newCol, moveNum+1)) {
return true;
}
}
board[row][col] = 0;
moveCount--;
return false;
}
int main() {
int startRow = 0, startCol = 0;
solve(startRow, startCol, 1);
cout << "Number of moves: " << moveCount << endl;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cout << board[i][j] << " ";
}
cout << endl;
}
return 0;
}