zoukankan      html  css  js  c++  java
  • 刷题 | Lintcode 611. Knight Shortest Path

    [Problem]

    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.

    Clarification

    If the knight is at (xy), 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

    [Idea]

    Examples:

    
    
    In above diagram Knight takes 3 step to reach from (4, 5) to (1, 1)
    (4, 5) -> (5, 3) -> (3, 2) -> (1, 1)  as shown in diagram

    This problem can be seen as shortest path in unweighted graph. Therefore we use BFS to solve this problem. We try all 8 possible positions where a Knight can reach from its position. If reachable position is not already visited and is inside the board, we push this state into queue with distance 1 more than its parent state. Finally we return distance of target position, when it gets pop out from queue.
    Below code implements BFS for searching through cells, where each cell contains its coordinate and distance from starting node. In worst case, below code visits all cells of board, making worst-case time complexity as O(N^2)

    [Code]

    /**
     * Definition for a point.
     * struct Point {
     *     int x, y;
     *     Point() : x(0), y(0) {}
     *     Point(int a, int b) : x(a), y(b) {}
     * };
     */
    class Solution {
    public:
        /**
         * @param grid a chessboard included 0 (false) and 1 (true)
         * @param source, destination a point
         * @return the shortest path 
         */
        int shortestPath(vector<vector<bool>>& grid, Point& source, Point& destination) {
            // Write your code here
            if(grid.empty() || grid[0].empty()) return -1;
            
            int m = grid.size();
            int n = grid[0].size();
            
            vector<vector<int>> directions = {{-2, -1}, {-2, 1}, {-1, 2}, {1, 2}, {2, 1}, {2, -1}, {1, -2}, {-1, -2}};
            
            vector<vector<int>> record(m, vector<int>(n, INT_MAX)); // record current distance, also plays a role to check if visited
            record[source.x][source.y] = 0;
    
            queue<Point> q;
            q.push(source);
            while (!q.empty()) {
                Point head = q.front(); 
                q.pop();
                for (int k = 0; k < 8; ++k) {
                    int x = head.x + directions[k][0];
                    int y = head.y + directions[k][1];
                    if (x >=0 && x < m && y >= 0 && y < n && !grid[x][y] &&
                        record[head.x][head.y] + 1 < record[x][y]) {
                        record[x][y] = record[head.x][head.y] + 1;
                        q.push(Point(x, y));
                    }
                }
            }
            if (record[destination.x][destination.y] == INT_MAX)
                return -1;
            return record[destination.x][destination.y];
        }
    };

    [Reference]

    1. GeeksForGeeks: Minimum steps to reach target by a Knight
    2. Jiuzhang Solution: Knight Shortest Path
  • 相关阅读:
    js四舍五入
    文本框只能输入整数,输入其他的自动不显示
    [转]关于C#程序部署到Android
    ajax在火狐中传中文出现乱码的解决方法
    Vue 记录 Cannot read property '_withTask' of undefined
    vs中 VMDebugger未能加载导致异常
    System.InvalidOperationException: 支持“XXX”上下文的模型已在数据库创建后发生更改。请考虑使用 Code First 迁移更新数据库(http://go.microsoft.com/fwlink/?LinkId=238269)。
    eclipse中将java项目转换成javaweb项目
    Android之SOAP协议与WebService服务器交互,解决超时的问题
    SymmetricDS 快速和灵活的数据库复制
  • 原文地址:https://www.cnblogs.com/casperwin/p/7455837.html
Copyright © 2011-2022 走看看