Knight Shortest Path

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.

Notice

source and destination must be empty.
Knight can not enter the barrier.

Have you met this question in a real interview? Yes
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 - 1, y + 2)
(x - 1, y - 2)
(x + 2, y + 1)
(x + 2, y - 1)
(x - 2, y + 1)
(x - 2, y - 1)
Example
[[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

java

/**
 * 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: a point
     * @param destination: a point
     * @return: the shortest path 
     */
    int[] dirX = {1, 1, -1, -1, 2, 2, -2, -2};
    int[] dirY = {2, -2, 2, -2, 1, -1, 1, -1};
    public int shortestPath(boolean[][] grid, Point source, Point destination) {
        // write your code here
        if(grid == null || grid.length == 0 || grid[0] == null || grid[0].length == 0) {
            return -1;
        }
        if (source.x == destination.x && source.y == destination.y) {
            return 0;
        }
        Queue<Point> queue = new LinkedList<>();
        int count = 0;
        queue.offer(source);
        while (!queue.isEmpty()) {
            int size = queue.size();
            count++;
            for (int i = 0; i < size; i++) {
                Point node = queue.poll();
                for (int j = 0; j < 8; j++) {
                    Point np = new Point(
                        node.x + dirX[j],
                        node.y + dirY[j]);
                    if (inBound(grid, np)) {
                        grid[np.x][np.y] = true;
                        if (np.x == destination.x && np.y == destination.y) {
                            return count;
                        }
                        queue.offer(np);
                    }
                }
            }
        }
        return -1;
    }
    private boolean inBound(boolean[][] grid, Point p) {
        int m = grid.length;
        int n = grid[0].length;
        if (p.x >= 0 && p.x < m && p.y >= 0 && p.y < n) {
            return grid[p.x][p.y] == false;
        }
        return false;
    }
}

python

"""
Definition for a point.
class Point:
    def __init__(self, a=0, b=0):
        self.x = a
        self.y = b
"""

import Queue

class Solution:
    """
    @param: grid: a chessboard included 0 (false) and 1 (true)
    @param: source: a point
    @param: destination: a point
    @return: the shortest path 
    """
    def shortestPath(self, grid, source, destination):
        # write your code here
        if grid is None or len(grid) == 0 or grid[0] is None or len(grid[0]) == 0:
            return -1
        if source.x == destination.x and source.y == destination.y:
            return 0
        queue, count = Queue.Queue(), 0
        queue.put(source)
        dirX = [1, 1, -1, -1, 2, 2, -2, -2]
        dirY = [2, -2, 2, -2, 1, -1, 1, -1]
        while not queue.empty():
            size = queue.qsize()
            count += 1
            for i in range(size):
                node = queue.get()
                for j in range(8):
                    np = Point(node.x + dirX[j],
                               node.y + dirY[j])
                    if self.inBound(grid, np):
                        if np.x == destination.x and np.y == destination.y:
                            return count
                        queue.put(np)
                        grid[np.x][np.y] = True
        return -1

    def inBound(self, grid, point):
        m, n = len(grid), len(grid[0])
        if point.x >= 0 and point.x < m and point.y >= 0 and point.y < n:
            return grid[point.x][point.y] == False
        return False
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ncst

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值