原题链接在这里: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的更多相关文章

  1. OpenJudge/Poj 1915 Knight Moves

    1.链接地址: http://bailian.openjudge.cn/practice/1915 http://poj.org/problem?id=1915 2.题目: 总Time Limit: ...

  2. Ural 1197 - Lonesome Knight

    The statement of this problem is very simple: you are to determine how many squares of the chessboar ...

  3. POJ 1915 Knight Moves(BFS+STL)

     Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 20913   Accepted: 9702 ...

  4. POJ-1915 Knight Moves (BFS)

    Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 26952   Accepted: 12721 De ...

  5. POJ 1915 Knight Moves

    POJ 1915 Knight Moves Knight Moves   Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 29 ...

  6. 917:Knight Moves

    题目链接:http://noi.openjudge.cn/ch0205/917/ 原题应该是hdu 1372 总时间限制: 1000ms  内存限制: 65536kB 描述 BackgroundMr ...

  7. 【广搜】Knight Moves

    题目描述 Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights fr ...

  8. Knight Moves

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  9. HDU 1372 Knight Moves

    最近在学习广搜  这道题同样是一道简单广搜题=0= 题意:(百度复制粘贴0.0) 题意:给出骑士的骑士位置和目标位置,计算骑士要走多少步 思路:首先要做这道题必须要理解国际象棋中骑士的走法,国际象棋中 ...

随机推荐

  1. Java学习:JDBC快速入门

    本节类容 JDBC基本概念 快速入门 JDBC基本概念 概念: Java DataBase Connectivity Java 数据库连接,Java语言操作数据库 JDBC本质: 其实是官方(sun公 ...

  2. NVDLA软件架构和源码解析 第一章—内核驱动【华为云技术分享】

    驱动整体设计介绍 不同的processor Nvidia DLA的内核驱动KMD(Kernel mode driver)中,并不是把DLA当成一个设备来控制,而是把不同的功能模块当做不同的proces ...

  3. 探索ASP.NET MVC5系列

    探索ASP.NET MVC5系列之~~~6.Session篇(进程外Session)     探索ASP.NET MVC5系列之~~~5.缓存篇(页面缓存+二级缓存)     探索ASP.NET MV ...

  4. CKEditor 4 上传视频

    参考资料:CKEditor添加Video视频插件 HTML5 video 记得配置插件以及上传地址,不知道哪一个是上传视频的地址参数,我也没有试,因为我有上传图片的功能,所以这三个参数我都设置了. / ...

  5. 从 Vue 的视角学 React(一)—— 项目搭建

    虽然 Vue 在国内一家独大,但在全球范围内,React 依然是最流行的前端框架 最近和一些朋友聊天,发现很多项目都选择了 React 技术栈,而且公司的新项目也决定使用 React 我一直以来都是走 ...

  6. shell:echo -e "\033字体颜色"

    格式: echo -e "\033[字背景颜色;字体颜色m字符串\033[0m" 例如: echo -e "\033[41;36m 你好 \033[0m" 其中 ...

  7. git https解决免ssL和保存密码

    1.打开windows的git bash set GIT_SSL_NO_VERIFY=true git clonegit config --global http.sslVerify false  2 ...

  8. tensorflow 镜像

    https://mirrors.tuna.tsinghua.edu.cn/tensorflow/windows/cpu/ 报错 不支持 C:\Users\brady\.conda\envs\tenso ...

  9. Spring MVC 上传、下载、显示图片

    目录 1. 准备工作 1.1 数据库表准备 1.2 实体类 User 和 Mapper(DAO) 1.3 pom.xml 依赖包 1.4 SSM 框架的整合配置 2. 控制器 UserControll ...

  10. Django 之瀑布流实现

    需求分析 现在是 "图片为王"的时代,在浏览一些网站时,经常会看到类似于这种满屏都是图片.图片大小不一,却按空间排列,就这是瀑布流布局. 以瀑布流形式布局,从数据库中取出图片 每次 ...