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 destinationposition, 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 (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

BFS solution:

 package fbOnsite;

 import java.util.*;

 public class KnightShortestPath {
public static int shortestPath(int[][] board, int[] src, int[] dst) {
int[][] directions = new int[][]{{1,2},{1,-2},{-1,2},{-1,-2},{2,1},{2,-1},{-2,1},{-2,-1}};
int m = board.length;
int n = board[0].length;
int res = 0; Queue<Integer> queue = new LinkedList<Integer>();
HashSet<Integer> visited = new HashSet<Integer>();
queue.offer(src[0]*n + src[1]);
while (!queue.isEmpty()) {
int size = queue.size();
for (int i=0; i<size; i++) {
int cur = queue.poll();
visited.add(cur);
int x = cur / n;
int y = cur % n;
if (x == dst[0] && y == dst[1]) return res; for (int[] dir : directions) {
int nx = x + dir[0];
int ny = y + dir[1];
if (nx<0 || nx>=m || ny<0 || ny>=n || visited.contains(nx*n+ny) || board[nx][ny]!=0)
continue;
queue.offer(nx*n + ny);
}
}
res++;
}
return res;
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] board = new int[][] {{0,1,0},{0,0,0},{0,0,0}};
int[] src = new int[]{2,0};
int[] dst = new int[]{2,2};
int res = shortestPath(board, src, dst);
System.out.println(res);
} }

Lintcode: Knight Shortest Path的更多相关文章

  1. hdu-----(2807)The Shortest Path(矩阵+Floyd)

    The Shortest Path Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  2. zoj 2760 How Many Shortest Path 最大流

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 Given a weighted directed graph ...

  3. The Shortest Path in Nya Graph

    Problem Description This is a very easy problem, your task is just calculate el camino mas corto en ...

  4. hdu 3631 Shortest Path(Floyd)

    题目链接:pid=3631" style="font-size:18px">http://acm.hdu.edu.cn/showproblem.php?pid=36 ...

  5. Shortest Path(思维,dfs)

    Shortest Path  Accepts: 40  Submissions: 610  Time Limit: 4000/2000 MS (Java/Others)  Memory Limit: ...

  6. Shortest Path

    Shortest Path Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  7. (中等) HDU 4725 The Shortest Path in Nya Graph,Dijkstra+加点。

    Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...

  8. 【ZOJ2760】How Many Shortest Path

    How Many Shortest Path 标签: 网络流 描述 Given a weighted directed graph, we define the shortest path as th ...

  9. [Swift]LeetCode847. 访问所有节点的最短路径 | Shortest Path Visiting All Nodes

    An undirected, connected graph of N nodes (labeled 0, 1, 2, ..., N-1) is given as graph. graph.lengt ...

随机推荐

  1. Class--2019-04-14

    获取class对象,有三种方法: 1.通过类名.class直接访问 Class c = Integer.class; 2.通过Class.forName(类名)函数获取 Class c = Class ...

  2. eclipse下启动tomcat访问网址报404错误

    问题: 解决步骤 首先检查一遍自己tomcat的相关配置,如果无误,则继续下面的操作 1.打开server视图,双击tomcat进入编辑页面 选择其中的第二个选项,并且修改Server path中的内 ...

  3. angularJs实现数据双向绑定的原理

    angular1.x在指定的事件触发时(比如dom事件,xhr响应事件,浏览器定位变更事件,定时器事件),通过脏值检测的方式比对数据是否有变更,来决定是否更新视图. angular2使用了zone.j ...

  4. Faster数据库研习,一

    什么是Faster   Faster 是一个很屌的嵌入式KeyValue 数据库项目   我简单的把 微软官网怎么吹的给大家翻译一下:   Faster:一个为状态管理而生的嵌入式并发KeyValue ...

  5. python 将list的值赋值给变量

    list中有n个值,赋值给k个变量:a1, a2, ……,ak n = k时:a1, a2, ……,ak = list n > k时:使用1式报错:ValueError: too many va ...

  6. Selenium 2自动化测试实战

    Selenium 2自动化测试实战 百度网盘 链接:https://pan.baidu.com/s/1aiP3d8Y1QlcHD3fAlEj4sg 提取码:jp8e 复制这段内容后打开百度网盘手机Ap ...

  7. SD卡

    一.SD卡接口 SD 卡的接口可以支持两种操作模式:主机系统可以选择以上其中任一模式, SD 卡模式允许 4 线的高速数据传输. SPI 模式允许简单通用的 SPI 通道接口, 这种模式相对于 SD ...

  8. Thinkphp3.2.3加载外部类并调用类里面的方法 获取token

    例如:加载七牛上传类(thinkphp自带的) $qiniu = new \Think\Upload\Driver\Qiniu\QiniuStorage($setting['driverConfig' ...

  9. [LeetCode] Cheapest Flights Within K Stops K次转机内的最便宜的航班

    There are n cities connected by m flights. Each fight starts from city u and arrives at v with a pri ...

  10. Java集合-treebag

    import org.apache.commons.collections4.Bag; import org.apache.commons.collections4.bag.TreeBag; impo ...