Skip to content

Commit dbb626d

Browse files
author
Ram swaroop
committed
coded + unit tested
1 parent be459ef commit dbb626d

File tree

2 files changed

+77
-6
lines changed

2 files changed

+77
-6
lines changed

src/me/ramswaroop/backtracking/KnightTour.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,20 @@
1515
public class KnightTour {
1616

1717
/**
18-
*
1918
* @param i
2019
* @param j
2120
* @param tour
2221
* @return
2322
*/
2423
public static boolean isValidMove(int i, int j, int[][] tour) {
25-
if (i >= 0 && j >= 0 && i < tour.length && j < tour[0].length && tour[i][j] == 0) {
24+
if (i >= 0 && i < tour.length && j >= 0 && j < tour[0].length && tour[i][j] == 0) {
2625
return true;
2726
} else {
2827
return false;
2928
}
3029
}
3130

3231
/**
33-
*
3432
* @param i
3533
* @param j
3634
* @param xMoves
@@ -43,9 +41,11 @@ public static boolean isValidKnightTour(int i, int j, int[] xMoves, int[] yMoves
4341

4442
if (step > tour.length * tour[0].length) return true;
4543

44+
int nextI, nextJ;
45+
4646
for (int k = 0; k < xMoves.length; k++) {
47-
int nextI = i + xMoves[k];
48-
int nextJ = j + yMoves[k];
47+
nextI = i + xMoves[k];
48+
nextJ = j + yMoves[k];
4949

5050
if (isValidMove(nextI, nextJ, tour)) {
5151
tour[nextI][nextJ] = step;
@@ -61,7 +61,6 @@ public static boolean isValidKnightTour(int i, int j, int[] xMoves, int[] yMoves
6161
}
6262

6363
/**
64-
*
6564
* @param boardSize
6665
*/
6766
public static void printKnightTour(int[] boardSize) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package me.ramswaroop.backtracking;
2+
3+
/**
4+
* Created by IntelliJ IDEA.
5+
*
6+
* @author: ramswaroop
7+
* @date: 10/18/15
8+
* @time: 10:39 AM
9+
*/
10+
public class RatInAMaze {
11+
12+
public static boolean isValidMove(int i, int j, int[][] maze) {
13+
if (i >= 0 && i < maze.length && j >= 0 && j < maze[0].length && maze[i][j] == 1) {
14+
return true;
15+
} else {
16+
return false;
17+
}
18+
}
19+
20+
public static boolean isValidPath(int i, int j, int[] xMoves, int[] yMoves, int[][] maze, int[][] path) {
21+
22+
if (i == maze.length - 1 && j == maze[0].length - 1) return true;
23+
24+
int nextI, nextJ;
25+
26+
for (int k = 0; k < xMoves.length; k++) {
27+
nextI = i + xMoves[k];
28+
nextJ = j + yMoves[k];
29+
if (isValidMove(nextI, nextJ, maze)) {
30+
path[nextI][nextJ] = 1;
31+
if (isValidPath(nextI, nextJ, xMoves, yMoves, maze, path)) {
32+
return true;
33+
} else {
34+
path[nextI][nextJ] = 0;
35+
}
36+
}
37+
}
38+
return false;
39+
}
40+
41+
public static void printMazePath(int i, int j, int[][] maze) {
42+
43+
int[] xMoves = {0, 1};
44+
int[] yMoves = {1, 0};
45+
46+
int[][] path = new int[maze.length][maze[0].length];
47+
48+
System.out.println("Maze");
49+
System.out.println("---------------");
50+
print2DMatrix(maze);
51+
System.out.println("---------------");
52+
53+
if (isValidPath(i, j, xMoves, yMoves, maze, path)) {
54+
print2DMatrix(path);
55+
} else {
56+
System.out.println("No escape path found!");
57+
}
58+
}
59+
60+
public static void print2DMatrix(int[][] array) {
61+
for (int i = 0; i < array.length; i++) {
62+
for (int j = 0; j < array[0].length; j++) {
63+
System.out.print("[" + array[i][j] + "]");
64+
}
65+
System.out.println();
66+
}
67+
}
68+
69+
public static void main(String a[]) {
70+
printMazePath(0, 0, new int[][]{{1, 1, 1, 1}, {0, 0, 1, 1}});
71+
}
72+
}

0 commit comments

Comments
 (0)