-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknight-tour-implementation.cpp
50 lines (49 loc) · 1.08 KB
/
knight-tour-implementation.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
#include <bits/stdc++.h>
using namespace std;
#define N 8
int mat[N][N];
//simple travelling that works for square matrix
int row[N] = {2, 1, -1, -2, -2, -1, 1, 2};
int col[N] = {1, 2, 2, 1, -1, -2, -2, -1};
bool isValid(int row, int column)
{
return (row >= 0 && column >= 0 && row < N && column < N && mat[row][column] == -1);
}
bool go(int r, int c, int move)
{
if (move == N * N)
return true;
int move_x, move_y;
for (int k = 0; k < N; k++)
{
move_x = r + row[k];
move_y = c + col[k];
if (isValid(move_x, move_y))
{
mat[move_x][move_y] = move + 1; //storing the move-number in matrix
if (go(move_x, move_y, move + 1) == 1)
return true;
else
mat[move_x][move_y] = -1; //backtracking
}
}
return false;
}
int main()
{
memset(mat, -1, sizeof(mat));
mat[0][0] = 1;
if (go(0, 0, 1))
{
//calling recur function and print the path matrix
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
cout << mat[i][j] << " ";
cout << endl;
}
}
else
cout << "Not possible\n";
return 0;
}