-
Notifications
You must be signed in to change notification settings - Fork 93
/
Warnsdorff’s algorithm_for_Knight’s_tour_problem
149 lines (125 loc) · 3.45 KB
/
Warnsdorff’s algorithm_for_Knight’s_tour_problem
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
// C++ program to for Kinight's tour problem usin
// Warnsdorff's algorithm
#include <bits/stdc++.h>
#define N 8
// Move pattern on basis of the change of
// x coordinates and y coordinates respectively
static int cx[N] = {1,1,2,2,-1,-1,-2,-2};
static int cy[N] = {2,-2,1,-1,2,-2,1,-1};
// function restricts the knight to remain within
// the 8x8 chessboard
bool limits(int x, int y)
{
return ((x >= 0 && y >= 0) && (x < N && y < N));
}
/* Checks whether a square is valid and empty or not */
bool isempty(int a[], int x, int y)
{
return (limits(x, y)) && (a[y*N+x] < 0);
}
/* Returns the number of empty squares adjacent
to (x, y) */
int getDegree(int a[], int x, int y)
{
int count = 0;
for (int i = 0; i < N; ++i)
if (isempty(a, (x + cx[i]), (y + cy[i])))
count++;
return count;
}
// Picks next point using Warnsdorff's heuristic.
// Returns false if it is not possible to pick
// next point.
bool nextMove(int a[], int *x, int *y)
{
int min_deg_idx = -1, c, min_deg = (N+1), nx, ny;
// Try all N adjacent of (*x, *y) starting
// from a random adjacent. Find the adjacent
// with minimum degree.
int start = rand()%N;
for (int count = 0; count < N; ++count)
{
int i = (start + count)%N;
nx = *x + cx[i];
ny = *y + cy[i];
if ((isempty(a, nx, ny)) &&
(c = getDegree(a, nx, ny)) < min_deg)
{
min_deg_idx = i;
min_deg = c;
}
}
// IF we could not find a next cell
if (min_deg_idx == -1)
return false;
// Store coordinates of next point
nx = *x + cx[min_deg_idx];
ny = *y + cy[min_deg_idx];
// Mark next move
a[ny*N + nx] = a[(*y)*N + (*x)]+1;
// Update next point
*x = nx;
*y = ny;
return true;
}
/* displays the chessboard with all the
legal knight's moves */
void print(int a[])
{
for (int i = 0; i < N; ++i)
{
for (int j = 0; j < N; ++j)
printf("%d\t",a[j*N+i]);
printf("\n");
}
}
/* checks its neighbouring sqaures */
/* If the knight ends on a square that is one
knight's move from the beginning square,
then tour is closed */
bool neighbour(int x, int y, int xx, int yy)
{
for (int i = 0; i < N; ++i)
if (((x+cx[i]) == xx)&&((y + cy[i]) == yy))
return true;
return false;
}
/* Generates the legal moves using warnsdorff's
heuristics. Returns false if not possible */
bool findClosedTour()
{
// Filling up the chessboard matrix with -1's
int a[N*N];
for (int i = 0; i< N*N; ++i)
a[i] = -1;
// Randome initial position
int sx = rand()%N;
int sy = rand()%N;
// Current points are same as initial points
int x = sx, y = sy;
a[y*N+x] = 1; // Mark first move.
// Keep picking next points using
// Warnsdorff's heuristic
for (int i = 0; i < N*N-1; ++i)
if (nextMove(a, &x, &y) == 0)
return false;
// Check if tour is closed (Can end
// at starting point)
if (!neighbour(x, y, sx, sy))
return false;
print(a);
return true;
}
// Driver code
int main()
{
// To make sure that different random
// initial positions are picked.
srand(time(NULL));
// While we don't get a solution
while (!findClosedTour())
{
;
}
return 0;
}