zoukankan      html  css  js  c++  java
  • 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:

     1 class Solution {
     2     int [][] dirs = new int[][]{{-1, -2}, {-1, 2}, {1, -2}, {1, 2}, {-2, -1}, {-2, 1}, {2, -1}, {2, 1}};
     3     
     4     public int minKnightMoves(int x, int y) {
     5         x = Math.abs(x);
     6         y = Math.abs(y);
     7         
     8         HashSet<String> visited = new HashSet<>();
     9         LinkedList<int []> que = new LinkedList<>();
    10         que.add(new int[]{0, 0});
    11         visited.add("0,0");
    12         
    13         int step = 0;
    14         while(!que.isEmpty()){
    15             int size = que.size();
    16             while(size-->0){
    17                 int [] cur = que.poll();
    18                 if(cur[0] == x && cur[1] == y){
    19                     return step;
    20                 }
    21 
    22                 for(int [] dir : dirs){
    23                     int i = cur[0] + dir[0];
    24                     int j = cur[1] + dir[1];
    25                     if(!visited.contains(i+","+j) && i>=-1 && j>=-1){
    26                         que.add(new int[]{i, j});
    27                         visited.add(i+","+j);
    28                     }
    29                 }
    30             }
    31             
    32             step++;
    33         }
    34         
    35         return -1;
    36     }
    37 }
  • 相关阅读:
    koa中静态文件资源中间件实现
    js数组扁平化
    js读写txt文件
    JDBC配置MSSQL
    Eclipse报错:Setting property 'source' to 'org.eclipse.jst.jee.server:xx' did not find a matching property
    JSON字符串的生成
    以POST方式推送JSON数据,并接收返回的服务器消息
    C# XML与Json之间相互转换实例详解
    eclipse配置Tomcat服务器server locations的方法
    uwsgi基础——SNMP
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11934828.html
Copyright © 2011-2022 走看看