Given a knight in a chessboard (a binary matrix with 0
as empty and 1
as barrier) with a source
position, find the shortest path to a destination
position, return the length of the route.
Return -1
if knight can not reached.
source and destination must be empty.
Knight can not enter the barrier.
If the knight is at (x, y), he can get to the following positions in one step:
(x + 1, y + 2)
(x + 1, y - 2)
(x - 1, y + 2)
(x - 1, y - 2)
(x + 2, y + 1)
(x + 2, y - 1)
(x - 2, y + 1)
(x - 2, y - 1)
[[0,0,0],
[0,0,0],
[0,0,0]]
source = [2, 0] destination = [2, 2] return 2
[[0,1,0],
[0,0,0],
[0,0,0]]
source = [2, 0] destination = [2, 2] return 6
[[0,1,0],
[0,0,1],
[0,0,0]]
source = [2, 0] destination = [2, 2] return -1
Solution. BFS, O(m * n) runtime, O(m * n) space
Instead of using an extra 2D boolean array to track if a cell has been visited or not, this solution does this tracking by marking visited cells as barriers.
The upside of doing this is to save some memory usage. The downside is the original input will be changed. This will be a problem if the shortestPath method needs to be called more than once.
1 /** 2 * Definition for a point. 3 * public class Point { 4 * publoc int x, y; 5 * public Point() { x = 0; y = 0; } 6 * public Point(int a, int b) { x = a; y = b; } 7 * } 8 */ 9 public class Solution { 10 /** 11 * @param grid a chessboard included 0 (false) and 1 (true) 12 * @param source, destination a point 13 * @return the shortest path 14 */ 15 protected int[] deltaX = {1, 1, -1, -1, 2, 2, -2, -2}; 16 protected int[] deltaY = {2, -2, 2, -2, 1, -1, 1, -1}; 17 public int shortestPath(boolean[][] grid, Point source, Point destination) { 18 if(grid == null || grid.length == 0 || grid[0].length == 0) 19 { 20 return -1; 21 } 22 int rowLen = grid.length; 23 int colLen = grid[0].length; 24 25 Queue<Point> queue = new LinkedList<Point>(); 26 queue.offer(source); 27 //mark visited cell as barrier 28 grid[source.x][source.y] = true; 29 int steps = 0; 30 31 while(queue.isEmpty() == false) 32 { 33 int size = queue.size(); 34 for(int i = 0; i < size; i++) 35 { 36 Point curr = queue.poll(); 37 if(curr.x == destination.x && curr.y == destination.y) 38 { 39 return steps; 40 } 41 //make all 8 possible moves 42 for(int direction = 0; direction < 8; direction++) 43 { 44 //check if the current direction is valid and has not been visited 45 if(isValidDirection(grid, curr.x + deltaX[direction], curr.y + deltaY[direction])) 46 { 47 queue.offer(new Point(curr.x + deltaX[direction], curr.y + deltaY[direction])); 48 //mark visited cell as barrier 49 grid[curr.x + deltaX[direction]][curr.y + deltaY[direction]] = true; 50 } 51 } 52 } 53 steps++; 54 } 55 return -1; 56 } 57 private boolean isValidDirection(boolean[][] grid, int row, int col) 58 { 59 int rowLen = grid.length; 60 int colLen = grid[0].length; 61 //check if in boundary 62 if(row < 0 || row >= rowLen || col < 0 || col >= colLen) 63 { 64 return false; 65 } 66 //check if barrier 67 return !grid[row][col]; 68 } 69 }
Related Problems
Knight Shortest Path II
Search Graph Nodes