-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathknighttour.c
184 lines (149 loc) · 4.11 KB
/
knighttour.c
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#if 0
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 100
int mat[MAX][MAX];
int num;
int IsSafe(int row, int col)
{
if (row <0 || row >num-1)
return 0;
if (col < 0 || col > num-1)
return 0;
if (mat[row][col] == -1)
return 1;
else
return 0;
}
int KnightTour(int moves, int row, int col , int x_moves[], int y_moves[])
{
int r, c, k;
if (moves == num*num)
return 1;
for (k=0; k<8; k++) {
int next_x = row + x_moves[k];
int next_y = col + y_moves[k];
if (IsSafe(next_x, next_y)) {
mat[next_x][next_y] = moves;
if (KnightTour(moves+1, next_x, next_y,x_moves, y_moves) == 1)
return 1;
else
mat[next_x][next_y]= -1;
}
}
return 0;
}
int main()
{
int r, c;
printf("Welcome to Knight's Tour..\n");
printf("Enter chess board configuration..");
scanf("%d", &num);
for(r=0; r <num; r++) {
for(c=0; c<num; c++) {
mat[r][c] = -1;
}
}
int x_moves[8] = {2, 2, -2, -2, -1, 1, -1, 1};
int y_moves[8] = {1, -1, 1, -1, 2, 2 -2, -2};
mat[0][0] = 0;
if (KnightTour(1,0, 0, x_moves, y_moves) == 1) {
printf("\n Time to print Chess Board Now...\n\n...\n");
for(r=0; r <num; r++) {
for(c=0; c<num; c++) {
printf("[%d]", mat[r][c]);
}
printf("\n");
}
}
}
#endif
//#if 0
#include <stdio.h>
#define N 8
#define bool int
#define true 1
#define false 0
int solveKTUtil(int x, int y, int movei, int sol[N][N], int xMove[],
int yMove[]);
/* A utility function to check if i,j are valid indexes for N*N chessboard */
int isSafe(int x, int y, int sol[N][N])
{
if ( x >= 0 && x < N && y >= 0 && y < N && sol[x][y] == -1)
return 1;
return 0;
}
/* A utility function to print solution matrix sol[N][N] */
void printSolution(int sol[N][N])
{
int x, y;
for (x = 0; x < N; x++)
{
for (y = 0; y < N; y++)
printf(" %2d ", sol[x][y]);
printf("\n");
}
}
/* This function solves the Knight Tour problem using Backtracking. This
function mainly uses solveKTUtil() to solve the problem. It returns false if
no complete tour is possible, otherwise return true and prints the tour.
Please note that there may be more than one solutions, this function
prints one of the feasible solutions. */
bool solveKT()
{
int sol[N][N], x, y;
/* Initialization of solution matrix */
for (x = 0; x < N; x++)
for (y = 0; y < N; y++)
sol[x][y] = -1;
/* xMove[] and yMove[] define next move of Knight.
xMove[] is for next value of x coordinate
yMove[] is for next value of y coordinate */
//int xMove[8] = { 2, 1, -1, -2, -2, -1, 1, 2 };
//int yMove[8] = { 1, 2, 2, 1, -1, -2, -2, -1 };
int xMove[8] = {2, 2, -2, -2, -1, 1, -1, 1};
int yMove[8] = {1, -1, 1, -1, 2, 2 -2, -2};
// Since the Knight is initially at the first block
sol[0][0] = 0;
/* Start from 0,0 and explore all tours using solveKTUtil() */
if(solveKTUtil(0, 0, 1, sol, xMove, yMove) == false)
{
printf("Solution does not exist");
return false;
}
else
printSolution(sol);
return true;
}
/* A recursive utility function to solve Knight Tour problem */
int solveKTUtil(int x, int y, int movei, int sol[N][N], int xMove[N],
int yMove[N])
{
int k, next_x, next_y;
if (movei == N*N)
return true;
/* Try all next moves from the current coordinate x, y */
for (k = 0; k < 8; k++)
{
next_x = x + xMove[k];
next_y = y + yMove[k];
if (isSafe(next_x, next_y, sol))
{
sol[next_x][next_y] = movei;
if (solveKTUtil(next_x, next_y, movei+1, sol, xMove, yMove) == true)
return true;
else
sol[next_x][next_y] = -1;// backtracking
}
}
return false;
}
/* Driver program to test above functions */
int main()
{
solveKT();
getchar();
return 0;
}
//#endif