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. Ubuntu16.04 将其他磁盘挂载到 /home, 解决/home空间不足

    本文转载自: https://blog.csdn.net/handsome_for_kill/article/details/52654724 1.查看磁盘信息 sudo fdisk -l 查看分区的 ...

  2. mybatis代码生成器——MyBatis Generator

    1.maven依赖 a.加入依赖 <!-- mybatis生成工具 --> <dependency> <groupId>org.mybatis.generator& ...

  3. 第十一篇 CBV和闪现

    前几篇写的都是FBV 现在可以了解一下CBV CBV 其实就是把请求方式都写到一个类中 学过django的一眼应该就明白了 from flask import Flask, render_templa ...

  4. [Web]Restful风格的适用场景

    最近一直在兜兜转转Restful的相关内容,准备在原先的项目上构建restful的API. 实践过程很别扭,直到看到这篇文章[A Brief Introduction to REST], 我才终于断定 ...

  5. InfluxDB——python使用手册

    InfluxDB--python使用手册 准备工作 安装InfluxDB: 请参考笔者相关博文:Centos7安装InfluxDB1.7 安装pip : yum install python-pip ...

  6. Python 中的单例模式

    单例模式 单例模式(Singleton Pattern)是一种常用的软件设计模式,该模式的主要目的是确保某一个类只有一个实例存在.当你希望在整个系统中,某个类只能出现一个实例时,单例对象就能派上用场. ...

  7. Android应用程序MVC框架实例分析

    问题提出:如何优雅地分离出应用程序的状态.用户交互和数据表现?如何通过框架体现工程的高性能.高灵活性.高响应性? MVC定义:model.view.controller三者的有机组合,分别表示:模型. ...

  8. __x__(26)0907第四天__文档流_网页最底层

    文档流 处在网页的最底层,表示的是一个页面中的位置. 创建的元素,默认都处于文档流中. 元素在文档流中的特点 块元素 在文档流中独占一行. 自上而下排列. 宽度默认占父元素的 100%,width=& ...

  9. mvc文件下载

    public ActionResult xiazai(int id) { DataTable dt = bll.chaxun(id); //获取文件名字 var filename = dt.Rows[ ...

  10. 如何使用post请求下载文件

    使用get请求下载文件非常简便,但是get请求的url有长度和大小的限制,所以当请求参数非常多时无法满足需求,所以改成post请求const res = await fetch('xxxxxxxxx' ...