-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnightShortPath.java
More file actions
97 lines (68 loc) · 2.41 KB
/
KnightShortPath.java
File metadata and controls
97 lines (68 loc) · 2.41 KB
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
package lintcode;
import java.util.LinkedList;
import java.util.Queue;
class Point{
int x;
int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
public class KnightShortPath {
public static void main(String[] args) {
boolean[][] grid = new boolean[3][3];
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
grid[i][j] = false;
}
}
Point source = new Point(2,0);
Point destination = new Point(2,2);
KnightShortPath shortPath = new KnightShortPath();
System.out.println(shortPath.shortestPath(grid, source,destination));
}
public int shortestPath(boolean[][] grid, Point source, Point destination) {
// write your code here
int[] deltaX = {1, 1, 2, 2, -1, -1, -2, -2};
int[] deltaY = {2, -2, 1, -1, 2, -2, 1, -1};
if (grid == null || grid.length == 0 || grid[0].length == 0) {
return -1;
}
int n = grid.length; // x length
int m = grid[0].length; // y length
Queue<Point> queue = new LinkedList<>();
queue.offer(source);
int steps = 0 ;
// bfs level order traversal
while (!queue.isEmpty()){
int size = queue.size();
for (int i = 0; i < size; i++) {
Point curr = queue.poll();
if(curr.x == destination.x && curr.y == destination.y){
return steps;
}
for (int j = 0; j < 8; j++) {
Point nextPoint = new Point( curr.x + deltaX[j], curr.y + deltaY[j]);
if( !inBound(nextPoint, grid)){
continue;
}
queue.offer(nextPoint);
// mark next point could not be accessed
grid[nextPoint.x][nextPoint.y] = true;
}
}
steps++;
}
return -1;
}
private boolean inBound(Point nextPoint, boolean[][] grid) {
if (nextPoint.x < 0 || nextPoint.x>= grid.length){
return false;
}
if (nextPoint.y<0 || nextPoint.y >= grid[0].length){
return false;
}
return grid[nextPoint.x][nextPoint.y] == false;
}
}