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) 题意:给出骑士的骑士位置和目标位置,计算骑士要走多少步 思路:首先要做这道题必须要理解国际象棋中骑士的走法,国际象棋中 ...
随机推荐
- swiper4基础
这段时间在公司实习做前端,感觉前端没学习到很多前端框架,接口那些都是写好的,只需要调用就好,感觉没什么好记的,唯一觉得有必要记的就是swiper轮播了,在前端做网站的时候经常用到swiper做公告,图 ...
- Space Syntax(空间句法)
01 December 2019 13:16 https://spacesyntax.com/ 相关软件:Depthmap 空间句法理论作为一种新的描述建筑和城市空间模式的语言 ...
- Ubuntu 下安装zsh和oh-my-zsh
注意:安装前先备份/etc/passwd 一开始装oh-my-zsh我是拒绝的,因为这东西安装容易,卸载难,真的很难. Mac安装参考:http://www.cnblogs.com/EasonJim/ ...
- [译] Ruby如何访问Excel文件
Parsing Excel Files with Ruby BY: MATT NEDRICH 翻译:佣工7001 本文中,我将会评判几种Ruby语言访问Excel文件的库.我将要讨论针对不同格式 ...
- .NET CORE 动态加载 DLL 的问题
有个系统, 需要适应不同类型的数据库(同时只使用其中一种),如果把数据库操作层提取出来,然后针对不同的数据库使用不同的 DLL, 再根据不同的项目使用不同的库, 在以前的 ASP.NET 中, 直接把 ...
- 24、vuex刷新页面数据丢失解决办法
刷新页面时候将state数据保存到localStorage里面: export default { name: 'App', created () { //在页面加载时读取localStorage里的 ...
- React学习笔记②
import React,{Component} from 'react'; import Child from './Child.js' class App extends Component{ c ...
- pythonic-迭代器函数-itertools
认识 Python 的itertools模块提供了很多节省内存的高效迭代器, 尤其解决了一些关于数据量太大而导致内存溢出(outofmemory)的场景. 我们平时用的循环绝大多数是这样的. # wh ...
- Java开发环境之Gradle
查看更多Java开发环境配置,请点击<Java开发环境配置大全> 拾伍章:Gradle安装教程 1)下载Gradle安装包 官网下载:https://gradle.org/releases ...
- spring cloud微服务实战教程/pdf/视频/百度云资源
资源站:http://www.supan.vip 点击进入直接查找资源: http://www.supan.vip/spring%20cloud微服务实战 <Spring Cloud微服务实战& ...