LeetCode 1197. Minimum Knight Moves
原题链接在这里:https://leetcode.com/problems/minimum-knight-moves/
题目:
In an infinite chess board with coordinates from -infinity to +infinity, you have a knight at square [0, 0].
A knight has 8 possible moves it can make, as illustrated below. Each move is two squares in a cardinal direction, then one square in an orthogonal direction.

Return the minimum number of steps needed to move the knight to the square [x, y]. It is guaranteed the answer exists.
Example 1:
Input: x = 2, y = 1
Output: 1
Explanation: [0, 0] → [2, 1]
Example 2:
Input: x = 5, y = 5
Output: 4
Explanation: [0, 0] → [2, 1] → [4, 2] → [3, 4] → [5, 5]
Constraints:
|x| + |y| <= 300
题解:
It is asking the minimum steps to get to (x, y). Thus, use BFS to iterate from (0, 0).
But how to make it faster. Since it is symmatic, we could focus on 1/4 directions.
Make x = |x|, y = |y|. Thus when doing BFS, besides checking it is visited, we also need to check it is within the boundary.
The bounday is >= -1. The reason it the shortest path may need the node on x =-1, y =-1. e.g. shortest path to (1, 1) is (0,0) -> (-1, 2) -> (1, 1).
Time Complexity: O(V+E). V is node count. E is edge count.
Space: O(V).
AC Java:
class Solution {
int [][] dirs = new int[][]{{-1, -2}, {-1, 2}, {1, -2}, {1, 2}, {-2, -1}, {-2, 1}, {2, -1}, {2, 1}};
public int minKnightMoves(int x, int y) {
x = Math.abs(x);
y = Math.abs(y);
HashSet<String> visited = new HashSet<>();
LinkedList<int []> que = new LinkedList<>();
que.add(new int[]{0, 0});
visited.add("0,0");
int step = 0;
while(!que.isEmpty()){
int size = que.size();
while(size-->0){
int [] cur = que.poll();
if(cur[0] == x && cur[1] == y){
return step;
}
for(int [] dir : dirs){
int i = cur[0] + dir[0];
int j = cur[1] + dir[1];
if(!visited.contains(i+","+j) && i>=-1 && j>=-1){
que.add(new int[]{i, j});
visited.add(i+","+j);
}
}
}
step++;
}
return -1;
}
}
LeetCode 1197. Minimum Knight Moves的更多相关文章
- OpenJudge/Poj 1915 Knight Moves
1.链接地址: http://bailian.openjudge.cn/practice/1915 http://poj.org/problem?id=1915 2.题目: 总Time Limit: ...
- Ural 1197 - Lonesome Knight
The statement of this problem is very simple: you are to determine how many squares of the chessboar ...
- POJ 1915 Knight Moves(BFS+STL)
Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 20913 Accepted: 9702 ...
- POJ-1915 Knight Moves (BFS)
Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 26952 Accepted: 12721 De ...
- POJ 1915 Knight Moves
POJ 1915 Knight Moves Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 29 ...
- 917:Knight Moves
题目链接:http://noi.openjudge.cn/ch0205/917/ 原题应该是hdu 1372 总时间限制: 1000ms 内存限制: 65536kB 描述 BackgroundMr ...
- 【广搜】Knight Moves
题目描述 Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights fr ...
- Knight Moves
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- HDU 1372 Knight Moves
最近在学习广搜 这道题同样是一道简单广搜题=0= 题意:(百度复制粘贴0.0) 题意:给出骑士的骑士位置和目标位置,计算骑士要走多少步 思路:首先要做这道题必须要理解国际象棋中骑士的走法,国际象棋中 ...
随机推荐
- 【chromium】 cef源码下载
至少需要17GB的磁盘空间,不光有CEF源码,还会下载chromium源码.编译master分支的话,如果编译到chromium可能会需要windows sdk,windows sdk的版本可以参考下 ...
- - instanceof 和 isInstance 强转 类型 class MD
目录 目录 instanceof 和 isInstance 强转 类型 class MD 简介 测试案例 继承关系 测试代码 打印结果 Markdown版本笔记 我的GitHub首页 我的博客 我的微 ...
- Azkaban 3.x 编译及部署
一.Azkaban 源码编译 1.1 下载并解压 Azkaban 在 3.0 版本之后就不提供对应的安装包,需要自己下载源码进行编译. 下载所需版本的源码,Azkaban 的源码托管在 GitHub ...
- K8S学习笔记之Kubernetes 配置管理 ConfigMap
0x00 概述 很多情况下我们为某一应用做好镜像,当我们想修改其中的一些参数的时候,就变得比较麻烦,又要重新制作镜像,我们是不是有一种方式,让镜像根据不同的场景调用我们不同的配置文件呢,那我们就需要用 ...
- 入门-windows下安装ETH挖矿
对刚入门的区块链开发者来说,刚开始可以在windows本地搭建私有链,便于操作,毕竟,要想真正挖到币还是有难度的,下面以ETH为例,在windows环境下安装并实现挖矿. 步骤一.安装geth环境.下 ...
- lnmp1.4安装包
https://lnmp.org/install.html nginx中虚拟机中的配置 location ~ .*\.(php|php5)?$ { try_files $uri =404; fastc ...
- React 根据条件自动计算
1.输入框 <Item {...formItemProps} label="留房日期"> {getFieldDecorator('date', { rules: [{ ...
- MFC 文件保存对话框的设置的那些秘密
搬家自CSDN 2015-5-14 CFileDialog::CFileDialog( BOOL bOpenFileDialog, LPCTSTR lpszDefExt = NULL, LPCTSTR ...
- MYSQL使用mysqldump导出表的部分数据
MySQLdump是MySQL自带的导出数据工具,通常我们用它来导出MySQL中,但是有时候我们需要导出MySQL数据库中某个表的部分数据,这时该怎么办呢? mysqldump命令中带有一个 --wh ...
- RxJS——订阅(Subscription)
订阅(Subscription) 什么是订阅?订阅是一个对象,它表示一个处理完就释放(disposable)的资源,是 Observable 的一个执行程序.订阅有一个很重要的方法,unsubscri ...