[LintCode] Knight Shortest Path

 Given a knight in a chessboard (a binary matrix with 0 as empty and 1 as barrier) with a sourceposition, find the shortest path to a destination position, return the length of the route. 

Return -1 if knight can not reached.

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

 

Solution. BFS, O(m * n) runtime, O(m * n) space 

Instead of using an extra 2D boolean array to track if a cell has been visited or not, this solution does this tracking by marking visited cells as barriers. 

The upside of doing this is to save some memory usage. The downside is the original input will be changed. This will be a problem if the shortestPath method needs to be called more than once.

 

 1 /**
 2  * Definition for a point.
 3  * public class Point {
 4  *     publoc int x, y;
 5  *     public Point() { x = 0; y = 0; }
 6  *     public Point(int a, int b) { x = a; y = b; }
 7  * }
 8  */
 9 public class Solution {
10     /**
11      * @param grid a chessboard included 0 (false) and 1 (true)
12      * @param source, destination a point
13      * @return the shortest path 
14      */
15     protected int[] deltaX = {1, 1, -1, -1, 2, 2, -2, -2};
16     protected int[] deltaY = {2, -2, 2, -2, 1, -1, 1, -1};
17     public int shortestPath(boolean[][] grid, Point source, Point destination) {
18         if(grid == null || grid.length == 0 || grid[0].length == 0)
19         {
20             return -1;
21         }
22         int rowLen = grid.length;
23         int colLen = grid[0].length;
24         
25         Queue<Point> queue = new LinkedList<Point>();
26         queue.offer(source);
27         //mark visited cell as barrier
28         grid[source.x][source.y] = true;
29         int steps = 0;
30         
31         while(queue.isEmpty() == false)
32         {
33             int size = queue.size();
34             for(int i = 0; i < size; i++)
35             {
36                 Point curr = queue.poll();
37                 if(curr.x == destination.x && curr.y == destination.y)
38                 {
39                     return steps;
40                 }
41                 //make all 8 possible moves
42                 for(int direction = 0; direction < 8; direction++)
43                 {
44                     //check if the current direction is valid and has not been visited
45                     if(isValidDirection(grid, curr.x + deltaX[direction], curr.y + deltaY[direction]))
46                     {
47                         queue.offer(new Point(curr.x + deltaX[direction], curr.y + deltaY[direction]));
48                         //mark visited cell as barrier
49                         grid[curr.x + deltaX[direction]][curr.y + deltaY[direction]] = true;
50                     }
51                 }
52             }
53             steps++;
54         }
55         return -1;
56     }
57     private boolean isValidDirection(boolean[][] grid, int row, int col)
58     {
59         int rowLen = grid.length;
60         int colLen = grid[0].length;
61         //check if in boundary
62         if(row < 0 || row >= rowLen || col < 0 || col >= colLen)
63         {
64             return false;
65         }
66         //check if barrier
67         return !grid[row][col];
68     }
69 }

 

 

 

Related Problems

Knight Shortest Path II

Search Graph Nodes

转载于:https://www.cnblogs.com/lz87/p/7496899.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值