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) 题意:给出骑士的骑士位置和目标位置,计算骑士要走多少步 思路:首先要做这道题必须要理解国际象棋中骑士的走法,国际象棋中 ...
随机推荐
- 用FTPClient,执行到ftp.storeFile(fileName, inputFile);无反应了
Q:用FTPClient,执行到ftp.storeFile(fileName, inputFile):无反应了 A: ftpclient.enterLocalPassiveMode(); ftp. ...
- c++中如何判断sqlite表是否存在
在项目中遇到需要判断sqlite数据库中某个表是否存在,上网搜索一些资料后,解决了问题,如下: 首先,在每个sqlite数据库中,都有一个名为sqlite_master的表,它定义了数据库的模式,它的 ...
- 大数据技术 - 为什么是SQL
在大数据处理以及分析中 SQL 的普及率非常高,几乎是每一个大数据工程师必须掌握的语言,甚至非数据处理岗位的人也在学习使用 SQL.今天这篇文章就聊聊 SQL 在数据分析中作用以及掌握 SQL 的必要 ...
- 关于vs无法创建虚拟目录的问题
插入链接:https://blog.csdn.net/zhao1955/article/details/92182935 补充:改完之后不要忘记以管理员的身份运行vs
- Delphi中HInstance
通过测试看出:HInstance.Application.Handle.Self.Handle不是一回事. Self.Handle是窗体句柄: Application.Handle也是个窗体的句柄,不 ...
- Orleans 3.0 为我们带来了什么(转载)
以下为本篇文章的 作者: 艾心 出处: https://www.cnblogs.com/edison0621/ 原文:https://devblogs.microsoft.com/dotnet/orl ...
- js之正则
1.正则的声明方法 1)var reg = /abc/; "这个叫对象直接量方式": 2)var reg = new RegExp("abc") 这个叫构造函数 ...
- javascript实现网页倒计时效果
一.HTML代码如下: <div class="timer" id="timer"> <span style="color: bla ...
- jsonserver的安装及启动
JsonServer 主要的作用就是搭建本地的数据接口,创建json文件,便于调试调用 是一个 Node 模块,运行 Express 服务器,可以指定一个 json 文件作为 api 的数据源 官网: ...
- Qt Graphics-View的打印功能实现
本文来研究一下Qt Graphics-View的打印功能实现. 在Qt的官方文档中介绍了Graphics-View的打印相关内容. Qt中对打印的支持是有一个独立的printsupport模块来完成的 ...