File tree 2 files changed +19
-11
lines changed
main/java/com/thealgorithms/backtracking
test/java/com/thealgorithms/backtracking 2 files changed +19
-11
lines changed Original file line number Diff line number Diff line change @@ -41,6 +41,23 @@ private KnightsTour() {
41
41
// Total number of cells the knight needs to visit
42
42
static int total ;
43
43
44
+ /**
45
+ * Resets the chess board to its initial state.
46
+ * Initializes the grid with boundary cells marked as -1 and internal cells as 0.
47
+ * Sets the total number of cells the knight needs to visit.
48
+ */
49
+ public static void resetBoard () {
50
+ grid = new int [BASE ][BASE ];
51
+ total = (BASE - 4 ) * (BASE - 4 );
52
+ for (int r = 0 ; r < BASE ; r ++) {
53
+ for (int c = 0 ; c < BASE ; c ++) {
54
+ if (r < 2 || r > BASE - 3 || c < 2 || c > BASE - 3 ) {
55
+ grid [r ][c ] = -1 ; // Mark boundary cells
56
+ }
57
+ }
58
+ }
59
+ }
60
+
44
61
/**
45
62
* Recursive method to solve the Knight's Tour problem.
46
63
*
Original file line number Diff line number Diff line change @@ -11,21 +11,12 @@ public class KnightsTourTest {
11
11
12
12
@ BeforeEach
13
13
void setUp () {
14
- // Reset the grid and total for each test
15
- KnightsTour .grid = new int [12 ][12 ];
16
- KnightsTour .total = (12 - 4 ) * (12 - 4 );
17
- for (int r = 0 ; r < 12 ; r ++) {
18
- for (int c = 0 ; c < 12 ; c ++) {
19
- if (r < 2 || r > 12 - 3 || c < 2 || c > 12 - 3 ) {
20
- KnightsTour .grid [r ][c ] = -1 ;
21
- }
22
- }
23
- }
14
+ // Call the reset method in the KnightsTour class
15
+ KnightsTour .resetBoard ();
24
16
}
25
17
26
18
@ Test
27
19
void testGridInitialization () {
28
- // Ensure that the border squares are -1 and internal grid is 0
29
20
for (int r = 0 ; r < 12 ; r ++) {
30
21
for (int c = 0 ; c < 12 ; c ++) {
31
22
if (r < 2 || r > 12 - 3 || c < 2 || c > 12 - 3 ) {
You can’t perform that action at this time.
0 commit comments