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.

說明

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

代碼實(shí)現(xiàn):

 * 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 {
    int n, m; // size of the chessboard
    //魔法數(shù)組
    int[] deltaX = {1, 1, 2, 2, -1, -1, -2, -2};
    int[] deltaY = {2, -2, 1, -1, 2, -2, 1, -1};
    /**
     * @param grid a chessboard included 0 (false) and 1 (true)
     * @param source, destination a point
     * @return the shortest path 
     */
    public int shortestPath(boolean[][] grid, Point source, Point destination) {
        if (grid == null || grid.length == 0 || grid[0].length == 0) {
            return -1;
        }
        
        n = grid.length;
        m = grid[0].length;
        
        Queue<Point> queue = new LinkedList<>();
        queue.offer(source);
        grid[source.x][source.y] = true; 
        int steps = 0;
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                Point point = queue.poll();
                if (point.x == destination.x && point.y == destination.y) {
                    return steps;
                }            
                for (int direction = 0; direction < 8; direction++) {
                    Point nextPoint = new Point(
                        point.x + deltaX[direction],
                        point.y + deltaY[direction]
                    );      
                    if (!inBound(nextPoint, grid)) {
                        continue;
                    }
                    if (grid[nextPoint.x][nextPoint.y] == false) {
                    queue.offer(nextPoint);
                    // 標(biāo)記point不可到達(dá)
                    grid[nextPoint.x][nextPoint.y] = true;
                    }
                }
            }
            //遍 歷完所有的下一跳節(jié)點(diǎn),steps++
            steps++;
        }     
        return -1;
    }
    //判斷是否在二維數(shù)組中
    private boolean inBound(Point point, boolean[][] grid) {
        return point.x >= 0 && point.x < grid.length && point.y >= 0
                  && point.y < grid[0].length; 
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,158評論 0 23
  • 代碼從零開始 你可以在本地創(chuàng)建一個(gè)空白的文件夾,然后克隆剛剛創(chuàng)建的項(xiàng)目(ps: clone url 在項(xiàng)目主頁的右...
    魔性佛心閱讀 292評論 1 1
  • 原文鏈接:https://grayj.co/post/a-product-is-a-tool-for-helpin...
    javenfang閱讀 1,216評論 0 2
  • 冬日雪痕 文/鄒航 手握季節(jié)的符咒 在光陰的隧道里踏歌前行 讓滿天雪花在指尖 融解內(nèi)心的憂愁 我看到了,冬日里的雪...
    鄒航閱讀 164評論 2 2

友情鏈接更多精彩內(nèi)容