-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKnightTour.java
96 lines (82 loc) · 3.39 KB
/
KnightTour.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
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int r = Integer.parseInt(br.readLine());
int c = Integer.parseInt(br.readLine());
int[][] chess = new int[n][n];
printKnightsTour(chess, r, c, 1);
}
public static void printKnightsTour(int[][] chess, int r, int c, int upcomingMove){
if(r < 0 || c < 0 || r >= chess.length || c >= chess.length || chess[r][c] != 0){
return;
} else if(upcomingMove == chess.length * chess.length){
chess[r][c] = upcomingMove;
displayBoard(chess);
chess[r][c] = 0;
return;
}
chess[r][c] = upcomingMove;
printKnightsTour(chess, r - 2, c + 1, upcomingMove + 1);
printKnightsTour(chess, r - 1, c + 2 , upcomingMove + 1);
printKnightsTour(chess, r + 1, c + 2 , upcomingMove + 1);
printKnightsTour(chess, r + 2, c + 1 , upcomingMove + 1);
printKnightsTour(chess, r + 2, c - 1 , upcomingMove + 1);
printKnightsTour(chess, r + 1, c - 2 , upcomingMove + 1);
printKnightsTour(chess, r - 1, c - 2 , upcomingMove + 1);
printKnightsTour(chess, r - 2, c - 1 , upcomingMove + 1);
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();
}
}
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int r = Integer.parseInt(br.readLine());
int c = Integer.parseInt(br.readLine());
int[][] chess = new int[n][n];
printKnightsTour(chess, r, c, 1);
}
public static void printKnightsTour(int[][] chess, int r, int c, int upcomingMove){
if(r < 0 || c < 0 || r >= chess.length || c >= chess.length || chess[r][c] != 0){
return;
} else if(upcomingMove == chess.length * chess.length){
chess[r][c] = upcomingMove;
displayBoard(chess);
chess[r][c] = 0;
return;
}
chess[r][c] = upcomingMove;
printKnightsTour(chess, r - 2, c + 1, upcomingMove + 1);
printKnightsTour(chess, r - 1, c + 2 , upcomingMove + 1);
printKnightsTour(chess, r + 1, c + 2 , upcomingMove + 1);
printKnightsTour(chess, r + 2, c + 1 , upcomingMove + 1);
printKnightsTour(chess, r + 2, c - 1 , upcomingMove + 1);
printKnightsTour(chess, r + 1, c - 2 , upcomingMove + 1);
printKnightsTour(chess, r - 1, c - 2 , upcomingMove + 1);
printKnightsTour(chess, r - 2, c - 1 , upcomingMove + 1);
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();
}
}