AcWing1219.移动距离——学习笔记

目录

题目

代码 

AC结果

思路:

一、获取数据

二、获取坐标

计算行号

计算列号

三、计算距离

四、输出


题目

1219. 移动距离 - AcWing题库icon-default.png?t=MBR7https://www.acwing.com/problem/content/description/1221/


代码 

import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        //获取数据
        int[] wmn = new int[3];
        Scanner input = new Scanner(System.in);
        for(int i = 0 ; i < 3; i++){
            wmn[i] = input.nextInt();
        }
        int[] mId = getIndex(wmn[1],wmn[0]);
        int[] nId = getIndex(wmn[2],wmn[0]);
        int ans = calculateDistance(mId,nId);

        System.out.println(ans);
    }
    //获取坐标
    public static int[] getIndex(int num,int w){
        int x = 0;
        int y =0;
        int remainder = num % w;
        int[] res = new int[2];
        if(remainder == 0){
            x = num / w;
        }else{
            x = (num / w) + 1;
        }
        res[0] = x;
        if(x % 2 == 0){
            y = (w - remainder + 1) > w ? (w - remainder + 1) % w : (w - remainder + 1);
        }else{
            y = remainder == 0 ? w : remainder;
        }
        res[1] = y;
        return res;
    }
    //计算距离
    public static int calculateDistance(int[] a, int [] b){
        int step = 0;
        step += Math.abs(a[1] - b[1]);
        step += Math.abs(a[0] - b[0]);
        return step;
    }
}

AC结果


思路:

输入三个数w,m,n,分别表示宽度、楼号1和楼号2。已知宽度和楼号,通过计算可以得出该楼号的对应坐标,以此计算两个楼号的对应坐标。知道两个坐标后,即可以计算其最短路径。

一、获取数据

        //获取数据
        int[] wmn = new int[3];
        Scanner input = new Scanner(System.in);
        for(int i = 0 ; i < 3; i++){
            wmn[i] = input.nextInt();
        }

将题目输入的三个数w、m、n存入到一个int类型的数组wmn中。

二、获取坐标

    //获取坐标
    public static int[] getIndex(int num,int w){
        int x = 0;
        int y =0;
        int remainder = num % w;
        int[] res = new int[2];
        if(remainder == 0){
            x = num / w;
        }else{
            x = (num / w) + 1;
        }
        res[0] = x;
        if(x % 2 == 0){
            y = (w - remainder + 1) > w ? (w - remainder + 1) % w : (w - remainder + 1);
        }else{
            y = remainder == 0 ? w : remainder;
        }
        res[1] = y;
        return res;
    }

计算行号

先判断当前楼号是否可以被宽度整除。若可以,则意味着计算出来的商即是该楼号坐标所在的行;若不可,就表示商的值再加一才是其所在的行。用remainder记录余数,若整除余数为0。

计算列号

因为x星球的楼房并不是按照常规顺序排列的,而是蛇形排列。因此,需要根据行的单双值判断那一行的从小到大顺序(单:从小到大排列;双:从大到小排列)。如果行号为双数,则其列号需要用宽度减去余数加一再对宽度取余(因为w-remainder+1的值有可能大于宽度w,这种时候需要对其取余保证数值合理正确)。如果行号为单号,其顺序就是从小到大排列,余数就是其列号。但是,需要注意一种特殊情况:单号行的最后一个数,他们的余数都为0。因此当单号行余数为0的时候,列号就是宽度。

三、计算距离

    //计算距离
    public static int calculateDistance(int[] a, int [] b){
        int step = 0;
        step += Math.abs(a[1] - b[1]);
        step += Math.abs(a[0] - b[0]);
        return step;
    }

经过第二步,得到两个楼号的对应坐标。因为题目需要求得最短路线,且不允许斜线走,所以此时只需要将两个坐标对应的列号和行号【分别相减】【取绝对值】【再相加】,就是最后需要走的步数。

四、输出

        int[] mId = getIndex(wmn[1],wmn[0]);
        int[] nId = getIndex(wmn[2],wmn[0]);
        int ans = calculateDistance(mId,nId);

        System.out.println(ans);

在主方法中调用上述两个方法,先调用getIndex分别得到两个楼号对应的坐标。然后,调用calculateDistance计算两者间最少步数。最后输出即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hokachi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值