原题链接在这里: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. 【转】Mac入门(一)基本用法

    我前五年一直外包到微软,每天使用的都是Windows系统和.NET. 2012年加入VMware,  公司的工作机是台Mac 笔记本(MacBook Pro), 所以有机会接触Mac系统 Mac和Wi ...

  2. Win 10下安装 Redis

    目录 写在前面 一.安装环境 二.下载windows版本的Redis 三.安装Redis 四.安装服务 五.启动服务 六.测试Redis 七.常用的Redis服务. 写在前面 Redis 是一个开源使 ...

  3. layout_gravity 属性和 gravity属性的区别

    安卓中  LinearLayout有两个非常相似的属性: android:gravity与android:layout_gravity. 区别在于: android:gravity 属性是对该view ...

  4. CodeForces 955D Scissors

    昨晚CF比赛比较颓,今天有心情写题解就不错了QWQ 洛谷题目页面传送门 & CodeForces题目页面传送门 给定字符串\(a,b,|a|=n,|b|=m\),求是否可以在\(a\)中选\( ...

  5. js原生Ajax(十四)

    一.XMLHttpRequest    [使用XMLHttpRequest时,必须将html部署到web服务器中]1) 指定请求1.实例化eg: var http = new XMLHttpReque ...

  6. 英语koreite寿山石koreite单词

    koreite指寿山石 寿山石是中华瑰宝,中国传统“四大印章石“之一.分布在福州市北郊晋安区与连江县.罗源县交界处的“金三角”地带. 寿山石是福州特有的名贵石材,其石质晶莹.脂润.色彩斑斓,色泽浑然天 ...

  7. kbmmw 中使用带验证的REST 服务

    前面介绍的rest 服务,虽然很方便,但是存在任何人都可以访问的安全问题. 今天说一下,如何在kbmmw 中使用带验证的REST 服务? 首先我们在工程中放一个 认证控件TkbmMWAuthoriza ...

  8. ORA-12514: 监听程序当前无法识别连接描述符中请求的服务

    /** 异常:ORA-12514: 监听程序当前无法识别连接描述符中请求的服务 * 背景:在很长一段时间都在连接远程开发库,曾偶尔有一次想要连接本地的库进行sql测试,发现连接失败,起初一直有无监听. ...

  9. rest framework 之序列化

    一.示例 restful work 的序列号就类似于 Django 的 Form 表单. 1.api/urls.py from django.urls import path, re_path fro ...

  10. Springboot 整合Activiti流程设计器 完成一个简单的请假流程

    目录 1.前言 2.准备 3.下载解压 4.开始整合 mysql + activiti + thymeleaf 2.配置文件 3.复制文件 4.加入控制器 5.修改配置文件 6.剔除启动类里面的安全校 ...