-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
KnightsTour.java
413 lines (355 loc) · 7.13 KB
/
KnightsTour.java
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/* Purpose: To check whether it is possible for a Knight to visit each cell of the N*N chessboard without visiting any cell twice starting from (X, Y) position.
Method: Backtracking Algorithm
Intution: To visit each and every positions which are available from the current position and recursively repeat this until all the cells are covered */
import java.io.*;
import java.util.*;
public class KnightsTour {
public static void main(String[] args) throws Exception {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int[][] chess = new int[n][n];
int r = scn.nextInt();
int c = scn.nextInt();
//call the function
knight( chess, r, c, 1 );
}
public static void knight (int[][] chess, int r, int c, int jump) {
//base case of recursion when the jump becomes equal to the length of check board
if( jump == chess.length * chess[0].length ){
//assign the jump number on the chess board
chess[r][c] = jump;
//call to display the feasible answer
displayBoard( chess );
//unvisit the cell
chess[r][c] = 0;
return;
}
//assign the jump number on the chess board
chess[r][c] = jump;
//recursive calls begins from here. It checks to see that the knight doesn't go out of the check board and also the cell should be unvisited
if( r - 2 >= 0 && c + 1 < chess[0].length && chess[ r-2 ][c+1] == 0 ){
knight( chess, r-2, c+1, jump+1 );
}
if( r - 1 >= 0 && c + 2 < chess[0].length && chess[ r-1 ][c+2] == 0 ){
knight( chess, r-1, c+2, jump+1 );
}
if( r + 1 < chess.length && c + 2 < chess[0].length && chess[ r+1 ][c+2] == 0 ){
knight( chess, r+1, c+2, jump+1 );
}
if( r + 2 < chess.length && c + 1 < chess[0].length && chess[ r+2 ][c+1] == 0 ){
knight( chess, r+2, c+1, jump+1 );
}
if( r + 2 < chess.length && c - 1 >= 0 && chess[ r+2 ][c-1] == 0 ){
knight( chess, r+2, c-1, jump+1 );
}
if( r + 1 < chess.length && c - 2 >= 0 && chess[ r+1 ][c-2] == 0 ){
knight( chess, r+1, c-2, jump+1 );
}
if( r - 1 >= 0 && c - 2 >= 0 && chess[ r-1 ][c-2] == 0 ){
knight( chess, r-1, c-2, jump+1 );
}
if( r - 2 >= 0 && c - 1 >= 0 && chess[ r-2 ][c-1] == 0 ){
knight( chess, r-2, c-1, jump+1 );
}
//unvisit the chess board. Backtracking here
chess[r][c] = 0;
}
public static void displayBoard(int[][] chess){
for(int i = 0; i < chess.length; i++){
for(int j = 0; j < chess[0].length; j++){
System.out.print(chess[i][j] + " ");
}
System.out.println();
}
System.out.println();
}
}
// There are N^2 Cells and for each, we have a maximum of 8 possible moves to choose from, so the worst running time is O(8N^2).
// When moving first precedence is given to (r - 2, c + 1) and the knight has moved in clockwise direction further.
/* Sample Input and Output-
Input:
5
2
0
Output is long since the number of possibilites is huge.
Output:
25 2 13 8 23
12 7 24 3 14
1 18 15 22 9
6 11 20 17 4
19 16 5 10 21
19 2 13 8 21
12 7 20 3 14
1 18 15 22 9
6 11 24 17 4
25 16 5 10 23
25 2 13 8 19
12 7 18 3 14
1 24 15 20 9
6 11 22 17 4
23 16 5 10 21
19 2 13 8 25
12 7 18 3 14
1 20 15 24 9
6 11 22 17 4
21 16 5 10 23
21 2 17 8 19
12 7 20 3 16
1 22 13 18 9
6 11 24 15 4
23 14 5 10 25
23 2 17 8 25
12 7 24 3 16
1 22 13 18 9
6 11 20 15 4
21 14 5 10 19
25 2 17 8 23
12 7 24 3 16
1 18 13 22 9
6 11 20 15 4
19 14 5 10 21
19 2 17 8 21
12 7 20 3 16
1 18 13 22 9
6 11 24 15 4
25 14 5 10 23
25 2 15 8 19
16 7 18 3 14
1 24 11 20 9
6 17 22 13 4
23 12 5 10 21
19 2 15 8 25
16 7 18 3 14
1 20 11 24 9
6 17 22 13 4
21 12 5 10 23
21 2 15 8 19
16 7 20 3 14
1 22 11 18 9
6 17 24 13 4
23 12 5 10 25
23 2 15 8 25
16 7 24 3 14
1 22 11 18 9
6 17 20 13 4
21 12 5 10 19
23 2 13 8 21
14 7 22 3 12
1 24 9 20 17
6 15 18 11 4
25 10 5 16 19
21 2 13 8 23
14 7 22 3 12
1 20 9 24 17
6 15 18 11 4
19 10 5 16 25
25 2 13 8 19
14 7 18 3 12
1 24 9 20 17
6 15 22 11 4
23 10 5 16 21
19 2 13 8 25
14 7 18 3 12
1 20 9 24 17
6 15 22 11 4
21 10 5 16 23
21 2 11 16 19
12 17 20 3 10
1 22 7 18 15
6 13 24 9 4
23 8 5 14 25
23 2 11 16 25
12 17 24 3 10
1 22 7 18 15
6 13 20 9 4
21 8 5 14 19
23 2 11 16 21
12 17 22 3 10
1 24 7 20 15
6 13 18 9 4
25 8 5 14 19
21 2 11 16 23
12 17 22 3 10
1 20 7 24 15
6 13 18 9 4
19 8 5 14 25
21 2 9 14 19
10 15 20 3 8
1 22 5 18 13
16 11 24 7 4
23 6 17 12 25
23 2 9 14 25
10 15 24 3 8
1 22 5 18 13
16 11 20 7 4
21 6 17 12 19
25 2 9 14 23
10 15 24 3 8
1 18 5 22 13
16 11 20 7 4
19 6 17 12 21
19 2 9 14 21
10 15 20 3 8
1 18 5 22 13
16 11 24 7 4
25 6 17 12 23
23 2 7 12 21
8 13 22 17 6
1 24 3 20 11
14 9 18 5 16
25 4 15 10 19
21 2 7 12 23
8 13 22 17 6
1 20 3 24 11
14 9 18 5 16
19 4 15 10 25
25 2 7 12 23
8 13 24 17 6
1 18 3 22 11
14 9 20 5 16
19 4 15 10 21
19 2 7 12 21
8 13 20 17 6
1 18 3 22 11
14 9 24 5 16
25 4 15 10 23
25 4 15 10 23
14 9 24 5 16
1 18 3 22 11
8 13 20 17 6
19 2 7 12 21
19 4 15 10 21
14 9 20 5 16
1 18 3 22 11
8 13 24 17 6
25 2 7 12 23
25 4 15 10 19
14 9 18 5 16
1 24 3 20 11
8 13 22 17 6
23 2 7 12 21
19 4 15 10 25
14 9 18 5 16
1 20 3 24 11
8 13 22 17 6
21 2 7 12 23
21 6 17 12 19
16 11 20 7 4
1 22 5 18 13
10 15 24 3 8
23 2 9 14 25
23 6 17 12 25
16 11 24 7 4
1 22 5 18 13
10 15 20 3 8
21 2 9 14 19
25 6 17 12 23
16 11 24 7 4
1 18 5 22 13
10 15 20 3 8
19 2 9 14 21
19 6 17 12 21
16 11 20 7 4
1 18 5 22 13
10 15 24 3 8
25 2 9 14 23
25 8 5 14 19
6 13 18 9 4
1 24 7 20 15
12 17 22 3 10
23 2 11 16 21
19 8 5 14 25
6 13 18 9 4
1 20 7 24 15
12 17 22 3 10
21 2 11 16 23
21 8 5 14 19
6 13 20 9 4
1 22 7 18 15
12 17 24 3 10
23 2 11 16 25
23 8 5 14 25
6 13 24 9 4
1 22 7 18 15
12 17 20 3 10
21 2 11 16 19
21 12 5 10 19
6 17 20 13 4
1 22 11 18 9
16 7 24 3 14
23 2 15 8 25
23 12 5 10 25
6 17 24 13 4
1 22 11 18 9
16 7 20 3 14
21 2 15 8 19
23 12 5 10 21
6 17 22 13 4
1 24 11 20 9
16 7 18 3 14
25 2 15 8 19
21 12 5 10 23
6 17 22 13 4
1 20 11 24 9
16 7 18 3 14
19 2 15 8 25
21 14 5 10 19
6 11 20 15 4
1 22 13 18 9
12 7 24 3 16
23 2 17 8 25
23 14 5 10 25
6 11 24 15 4
1 22 13 18 9
12 7 20 3 16
21 2 17 8 19
25 14 5 10 23
6 11 24 15 4
1 18 13 22 9
12 7 20 3 16
19 2 17 8 21
19 14 5 10 21
6 11 20 15 4
1 18 13 22 9
12 7 24 3 16
25 2 17 8 23
23 16 5 10 21
6 11 22 17 4
1 24 15 20 9
12 7 18 3 14
25 2 13 8 19
21 16 5 10 23
6 11 22 17 4
1 20 15 24 9
12 7 18 3 14
19 2 13 8 25
25 16 5 10 23
6 11 24 17 4
1 18 15 22 9
12 7 20 3 14
19 2 13 8 21
19 16 5 10 21
6 11 20 17 4
1 18 15 22 9
12 7 24 3 14
25 2 13 8 23
23 10 5 16 21
6 15 22 11 4
1 24 9 20 17
14 7 18 3 12
25 2 13 8 19
21 10 5 16 23
6 15 22 11 4
1 20 9 24 17
14 7 18 3 12
19 2 13 8 25
25 10 5 16 19
6 15 18 11 4
1 24 9 20 17
14 7 22 3 12
23 2 13 8 21
19 10 5 16 25
6 15 18 11 4
1 20 9 24 17
14 7 22 3 12
21 2 13 8 23 */