Saturday, April 8, 2017

Knight Shortest Path

Version #1 BFS
/**
 * Definition for a point.
 * public class Point {
 *     publoc int x, y;
 *     public Point() { x = 0; y = 0; }
 *     public Point(int a, int b) { x = a; y = b; }
 * }
 */
public class Solution {
    /**
     * @param grid a chessboard included 0 (false) and 1 (true)
     * @param source, destination a point
     * @return the shortest path
     */
    public boolean EMPTY = false;
    public int shortestPath(boolean[][] grid, Point source, Point destination) {
        // Write your code here
        int sp = -1;
        if (grid == null) {
            return sp;
        }
       
        sp = bfs(grid, source, destination);
        return sp;
    }
    public int bfs(boolean[][] grid, Point source, Point destination) {
        //int[] dX = {0, 1, 1, 1, 0, -1, -1, -1};
        //int[] dY = {1, 1, 0, -1, -1, -1, 0, 1};
        int[] dX = {1, 1, 2, 2, -1, -1, -2, -2};
        int[] dY = {2, -2, 1, -1, 2, -2, 1, -1};
        int level = 0;
     
     
        Queue<Point> que = new LinkedList<Point>();
        boolean[][] visited = new boolean[grid.length][grid[0].length];
        if (!isValid(grid, source)) {
            return -1;
        }
        if (!isValid(grid, destination)) {
            return -1;
        }
        //source = new Point(grid.length - source.x - 1, grid[0].length - source.y - 1);
        //destination = new Point(grid.length - destination.x - 1, grid[0].length - destination.y - 1);
        que.offer(source);
        visited[source.x][source.y] = true;
   
        while(!que.isEmpty()) {
            int levelSize = que.size();
            level++;
            for (int l = 0; l < levelSize; l++) {
                 Point point = que.poll();
                 //System.out.println("x = " + point.x + " y = " + point.y);
                 for (int i = 0; i < 8; i++) {
                     int nX = point.x + dX[i];
                     int nY = point.y + dY[i];
                     Point neighbor = new Point(nX, nY);
                     if (isValid(grid, neighbor) && !visited[nX][nY]) {
                         //System.out.println("nx = " + nX + " ny = " + nY + "level = " + level);
                         if (nX == destination.x && nY == destination.y) {
                             return level;
                         } else {
                             que.offer(neighbor);
                             visited[nX][nY] = true;
                         }
                     }
                 }
            }
        }
        return -1;
       
    }
    //grid[x][y]
    public boolean isValid(boolean[][] grid, Point point) {
        int x = point.x;
        int y = point.y;
        //System.out.println("x = " + x + " y = " + y);
        //System.out.println(grid.length + "" + grid[0].length);
        if (x >= 0 && x < grid.length && y >= 0 && y < grid[0].length) {
            return grid[x][y] == EMPTY;
        } else {
            return false;
        }
       
    }
}


Version #2 DP

No comments:

Post a Comment