[LintCode] Knight Shortest Path II

Given a knight in a chessboard n * m (a binary matrix with 0 as empty and 1 as barrier). the knight initialze position is (0, 0) and he wants to reach position (n - 1, m - 1), Knight can only be from left to right. Find the shortest path to the destination position, return the length of the route. Return -1 if knight can not reached.

Clarification

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 + 2, y + 1)
(x - 2, y + 1)
Example
[[0,0,0,0],
 [0,0,0,0],
 [0,0,0,0]]

Return 3

[[0,0,0,0],
 [0,0,0,0],
 [0,1,0,0]]

Return -1


思路1:
和八坐标很像,都是用bfs来做

public class Solution {
    private class Point {
        int x, y;
        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }
    };
    
    private static boolean BARRIER = true;
    public int shortestPath2(boolean[][] grid) {
        // write your code here
        if (grid == null || grid.length == 0) {
            return -1;
        }
        if (grid[0] == null || grid[0].length == 0) {
            return -1;
        }

        int step = countStep(grid);
        return step;
    }

    private int countStep(boolean[][] grid) {
        int[] X = {1, -1, 2, -2};
        int[] Y = {2, 2, 1, 1};
        int row = grid.length;
        int col = grid[0].length;
        Queue  q = new LinkedList<>();
        q.offer(new Point(0, 0));
        int step = 0;
        grid[0][0] = BARRIER;
        while (!q.isEmpty()) {
            int size = q.size();
            for (int i = 0; i < size; i++) {
                Point p = q.poll();
                System.out.println(p.x + " " + p.y + " " + step);
                if (p.x == row - 1 && p.y == col - 1) return step;
                for (int j = 0; j < X.length; j++) {
                    Point np = new Point(p.x + X[j], p.y + Y[j]);
                    if (isAvaliable(np, grid)) {
                        //System.out.println(np.x + " " + np.y);
                        q.offer(np);
                        grid[np.x][np.y] = BARRIER;
                    }
                }
            }
            step++;
        }
        return -1;
    }
    
    private boolean isAvaliable(Point p, boolean[][] grid) {
        if (p.x < 0 || p.x >= grid.length) return false;
        if (p.y < 0 || p.y >= grid[0].length) return false;
        if (grid[p.x][p.y] == BARRIER) return false;
        return true;
    }
}

思路二:

用动态规划来做,这道题其实是指定了方向,就是只能向右。是有顺训的,求最小值,可以考虑用动态规划来做。这个应该是坐标型动态规划的题目。时间复杂度和bfs一样都是O(mn)

dp[i][j] 代表在(i,j)的最小路径数目。先初始化这个dp,让每个值都是最大值。根据题意,dp[0][0] = 0, 根据给定的坐标可以知道,对于每一个循环的点,都可以求出它能从哪个点得来(反推)。这样我们可以知道dp[i][j]就等于它自身和它来源点dp值+1的比较的最小值。那些check的条件主要是用来看是不是在边界内,以及是不是有访问过。如果没有访问过,说明这些点根本就不可能成为现在的点的起源。

public class Solution {
    /*
     * @param grid: a chessboard included 0 and 1
     * @return: the shortest path
     */
    public int shortestPath2(boolean[][] grid) {
        // write your code here
        if (grid == null || grid.length == 0) {
            return -1;
        }
        if (grid[0] == null || grid[0].length == 0) {
            return -1;
        }
        
        int row = grid.length;
        int col = grid[0].length;
        
        int[][] dp = new int[row][col];
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                dp[i][j] = Integer.MAX_VALUE;
            }
        }
        dp[0][0] = 0;
        
        for (int j = 1; j < col; j++) {
            for (int i = 0; i < row; i++) {
                if (!grid[i][j]) {
                    if (i >= 1 && j >= 2 && dp[i - 1][j - 2] != Integer.MAX_VALUE)
                        dp[i][j] = Math.min(dp[i][j], dp[i - 1][j - 2] + 1);
                    if (i + 1 < row && j >= 2 && dp[i + 1][j - 2] != Integer.MAX_VALUE)
                        dp[i][j] = Math.min(dp[i][j], dp[i + 1][j - 2] + 1);
                    if (i >= 2 && j >= 1 && dp[i - 2][j - 1] != Integer.MAX_VALUE) 
                        dp[i][j] = Math.min(dp[i][j], dp[i - 2][j - 1] + 1);
                    if (i + 2 < row && j >= 1 && dp[i + 2][j - 1] != Integer.MAX_VALUE)
                        dp[i][j] = Math.min(dp[i][j], dp[i + 2][j - 1] + 1);
                }
            }
        }
        if (dp[row - 1][col - 1] == Integer.MAX_VALUE)
            return -1;
        return dp[row - 1][col - 1];
    }
}

 

 

你可能感兴趣的:([LintCode] Knight Shortest Path II)