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 ...
随机推荐
- docker - 容器lxc
容器:是在用户空间进行隔离的组件叫做容器 常用的容器有lxc ----libcontainer---runc 需要隔离的资源有: Rootfs:每个容器对应的一个目录做为根目录 User: Hostn ...
- celery使用
1.常用命令 (1)启动后台职程 celery worker -A tasks --loglevel=info celery worker -A tasks --loglevel= -A 是指cele ...
- 还不知道spring的RestTemplate的妙用吗
为什么要使用RestTemplate? 随着微服务的广泛使用,在实际的开发中,客户端代码中调用RESTful接口也越来越常见.在系统的遗留代码中,你可能会看见有一些代码是使用HttpURLConnec ...
- [wordpress]更新插件时,免去FTP操作
我们先进入服务器 先找到wordpress配置文件wp-config.php,用locate命令寻找文件所在路径. sudo updatedb locate wp-config.php 然后cd到改路 ...
- Python中re的match、search、findall、finditer区别
原文地址: http://blog.csdn.net/djskl/article/details/44357389 这四个方法是从某个字符串中寻找特定子串或判断某个字符串是否符合某个模式的常用方法. ...
- BZOJ.2823.[AHOI2012]信号塔(最小圆覆盖 随机增量法)
BZOJ 洛谷 一个经典的随机增量法,具体可以看这里,只记一下大体流程. 一个定理:如果一个点\(p\)不在点集\(S\)的最小覆盖圆内,那么它一定在\(S\bigcup p\)的最小覆盖圆上. 所以 ...
- visual studio的包含目录配置问题
早上将一个项目从debug x64修改到release x64,然后包含目录都是直接从debug拷贝过来的,一模一样的路径,一直说无法include,并且路径中的文件都是存在的,配置页面的releas ...
- XVIII Open Cup named after E.V. Pankratiev. Grand Prix of Saratov
A. Three Arrays 枚举每个$a_i$,双指针出$b$和$c$的范围,对于$b$中每个预先双指针出$c$的范围,那么对于每个$b$,在对应$c$的区间加$1$,在$a$处区间求和即可. 树 ...
- CSS(三)
CSS盒子模型 盒子模型解释 元素在页面中显示成一个方块,类似一个盒子,CSS盒子模型就是使用现实中盒子来做比喻,帮助我们设置元素对应的样式.盒子模型示意图如下: 把元素叫做盒子,设置对应的样式分别为 ...
- PropertyEditor、Formatter、Converter的应用
@ResponseBody @RequestMapping("date.do") public String data(Date date) { return date.toStr ...