[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 initial position is (0, 0) and he wants to reach position (n - 1, m - 1). 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

 

Analysis: This problem is different with Knight Shortest Path in that the knight can only move from left to right, not vice versa. This change makes dynamic programming solution possible as current subproblem's result only depends on the results of subproblems that have been computed. 

 

Solution 1. BFS

Same with Knight Shortest Path solution with only 4 possible directions to move.

 

 1 //Algorithm 1. BFS
 2 class Point{
 3     protected int x;
 4     protected int y;
 5     public Point(int x, int y){
 6         this.x = x;
 7         this.y = y;
 8     }
 9 }
10 public class Solution {
11     private int[] deltaX = {1, -1, 2, -2};
12     private int[] deltaY = {2, 2, 1, 1};
13     
14     public int shortestPath2(boolean[][] grid) {
15         if(grid == null || grid.length == 0 || grid[0].length == 0){
16             return -1;
17         }
18         Queue<Point> queue = new LinkedList<Point>();
19         queue.add(new Point(0, 0));
20         grid[0][0] = true;
21         int len = 0;
22         
23         while(queue.isEmpty() == false){
24             int size = queue.size();
25             for(int i = 0; i < size; i++){
26                 Point curr = queue.poll();
27                 if(curr.x == grid.length - 1 && curr.y == grid[0].length - 1){
28                     return len;
29                 }
30                 for(int dir = 0; dir < 4; dir++){
31                     int nextX = curr.x + deltaX[dir];
32                     int nextY = curr.y + deltaY[dir];
33                     if(isDirectionValid(grid, nextX, nextY)){
34                         queue.add(new Point(nextX, nextY));
35                         grid[nextX][nextY] = true;
36                     }    
37                 }
38             }
39             len++;
40         }
41         return -1;
42     }
43     private boolean isDirectionValid(boolean[][] grid, int x, int y){
44         return !(x < 0 || x >= grid.length || y < 0 || y >= grid[0].length || grid[x][y]);
45     }
46 }

 

Solution 2. Top Down Dynamic Programming

State: f[i][j]: the minimum number of moves needed from (0, 0) to (i, j)

 

 1 public class Solution {
 2     private int[] deltaX = {-1, 1, -2, 2};
 3     private int[] deltaY = {-2, -2, -1, -1};
 4     public int shortestPath2(boolean[][] grid) {
 5         if(grid == null || grid.length == 0 || grid[0].length == 0){
 6             return -1;
 7         }
 8         int n = grid.length;
 9         int m = grid[0].length;
10         int[][] f = new int[n][m];
11         
12         for(int i = 0; i < n; i++){
13             for(int j = 0; j < m; j++){
14                 f[i][j] = Integer.MAX_VALUE;    
15             }
16         }
17         
18         f[0][0] = 0;
19         
20         for(int j = 0; j < m; j++){
21             for(int i = 0; i < n; i++){
22                 if(grid[i][j] == false){
23                     int min = Integer.MAX_VALUE;
24                     for(int dir = 0; dir < 4; dir++){
25                         int x = i + deltaX[dir];
26                         int y = j + deltaY[dir];
27                         if(isDirectionValid(grid, x, y)){
28                             if(f[x][y] < min){
29                                 min = f[x][y];
30                             }    
31                         }
32                     }
33                     if(min < Integer.MAX_VALUE){
34                         f[i][j] = 1 + min;
35                     } 
36                 }
37 
38             }
39         }
40         return f[n - 1][m - 1] == Integer.MAX_VALUE ? -1 : f[n - 1][m - 1];
41     }
42     
43     private boolean isDirectionValid(boolean[][] grid, int x, int y){
44         return !(x < 0 || x >= grid.length || y < 0 || y >= grid[0].length);
45     }
46 }

 

 

Related Problems

Knight Shortest Path

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值