Lintcode: Knight Shortest Path
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的更多相关文章
- hdu-----(2807)The Shortest Path(矩阵+Floyd)
The Shortest Path Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- zoj 2760 How Many Shortest Path 最大流
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 Given a weighted directed graph ...
- The Shortest Path in Nya Graph
Problem Description This is a very easy problem, your task is just calculate el camino mas corto en ...
- hdu 3631 Shortest Path(Floyd)
题目链接:pid=3631" style="font-size:18px">http://acm.hdu.edu.cn/showproblem.php?pid=36 ...
- Shortest Path(思维,dfs)
Shortest Path Accepts: 40 Submissions: 610 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: ...
- Shortest Path
Shortest Path Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- (中等) 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 ...
- 【ZOJ2760】How Many Shortest Path
How Many Shortest Path 标签: 网络流 描述 Given a weighted directed graph, we define the shortest path as th ...
- [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 ...
随机推荐
- tensorflow结果可视化-【老鱼学tensorflow】
这次我们把上次的结果进行可视化显示,我们会把神经网络的优化过程以图像的方式展示出来,方便我们了解神经网络是如何进行优化的. 首先,我们把测试数据显示出来: # 显示测试数据 fig = plt.fig ...
- UOJ#405. 【IOI2018】组合动作
原文链接https://www.cnblogs.com/zhouzhendong/p/IOI2018Day1T1.html 题解 首先二分一下,花费2次操作求出第一位的字符. 假设第一个字符是 Y,答 ...
- aop切入mapper接口
***************************************分割线****************************************************** 参考: ...
- Docker操作笔记(二)容器
容器 一.启动容器 启动一个容器有两种方式: 1.基于镜像新键并启动一个容器: 所需要的主要命令为docker run docker run ubuntu:18.04 /bin/echo " ...
- Django——RESTful架构
一.REST简述 来自维基百科的解释: 表现层状态转换(REST,英文:Representational State Transfer)是Roy Thomas Fielding博士于2000年在他的博 ...
- linux(ubuntu) 安装composer(PHP用来管理依赖关系的工具 ) 和安装中国全量镜像
https://www.phpcomposer.com/ composer中文网 1:进入安装目录 cd /usr/local/bin 2:下载并安装 sudo curl -s http ...
- JZOJ5431 捕老鼠
JZOJ 5341 Description 为了加快社会主义现代化,建设新农村,农夫约(Farmer Jo)决定给农庄里的仓库灭灭鼠.于是,猫被农夫约派去捕老鼠. 猫虽然擅长捕老鼠,但是老鼠们太健美了 ...
- Yii2 验证规则
验证器的使用方法: public function rules() { return [ [['email', 'password'], 'required'], ['password', 'stri ...
- JTS相关资料和示例
示例 JTS基本概念和使用 JTS Geometry之间的关系 JTS algorithm package
- connect socket的超时设置
最近项目中,有个需求是检测某ip地址是否是通的,使用了socket的connect函数.但是,当ip地址写错的话,connect就会一直阻塞在那里,大概2.3分钟才能返回连接失败.这对于用户来说是不可 ...