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) 题意:给出骑士的骑士位置和目标位置,计算骑士要走多少步 思路:首先要做这道题必须要理解国际象棋中骑士的走法,国际象棋中 ...
随机推荐
- C语言创建链表
一.链表中结点的存储 链表的结点左边一部分是存放的数据,右边一部分是后继指针指向下一个结点的地址.C语言中通常定义一个结构体类型来存储一个结点,如下: struct node { int data; ...
- 【题解】Largest Rectangle in a Histogram [SP1805] [POJ2559]
[题解]Largest Rectangle in a Histogram [SP1805] [POJ2559] [题目描述] 传送: \(Largest\) \(Rectangle\) \(in\) ...
- SQL分类之DDL:操作数据库表
DDL:操作数据库表 1.操作数据库:CRUD 1.C(Create):创建 创建数据库: create database 数据库名称 创建数据库,判断不存在,再创建: create database ...
- sqlserver apply
IF OBJECT_ID('tb') IS NOT NULL DROP TABLE tb go CREATE TABLE tb(name VARCHAR(10),value VARCHAR(200)) ...
- 50道Java线程面试题分析及答案
下面是Java线程相关的热门面试题,你可以用它来好好准备面试. 1) 什么是线程?线程是操作系统能够进行运算调度的最小单位,它被包含在进程之中,是进程中的实际运作单位.程序员可以通过它进行多处理器编程 ...
- 创建.net framework webapi出现“Web 服务器被配置为不列出此目录的内容。”错误
接了一个新任务,要求写一个web api.于是我创建了一个.net framework的web api,结果在运行的时候,出现了以下页面: 解决方法: 在web.config文件中添加<dire ...
- Java Mockito 笔记
Mockito 1 Overview 2 Maven 项目初始化 3 示例 3.1 第一个示例 3.2 自动 Mock 3.3 Mock 返回值 3.4 Mock 参数 3.5 自动注入 Mock 对 ...
- 调用WebApi出现 远程服务器返回错误: (500) 内部服务器错误
一.检查错误错误 将 HttpWebResponse response = (HttpWebResponse)request.GetResponse();改为 HttpWebResponse resp ...
- 解决微信web页面键盘收起不回弹,导致按钮失效
在文本框失去焦点时加入以下代码 $('input,textarea').blur(function () { setTimeout(function(){ window.scrollTo(,docum ...
- Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile
更换当前jdk版本为项目所需jdk版本即可