AcWing 1219. 移动距离 (水题)

将点数转换成坐标即可

Problem

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

class Main {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter pw = new PrintWriter(System.out);
    static int n, a, b;

    public static void main(String[] args) throws IOException {
        String[] s = br.readLine().split(" ");
        n = Integer.parseInt(s[0]);
        a = Integer.parseInt(s[1]);
        b = Integer.parseInt(s[2]);

        a--;
        b--;
        int ax = a / n;
        int bx = b / n;
        int ay = a % n;
        int by = b % n;
        if ((ax & 1) == 0) ay = n - ay - 1;
        if ((bx & 1) == 0) by = n - by - 1;
        pw.print(Math.abs(ax - bx) + Math.abs(ay - by));

        pw.flush();
        pw.close();
        br.close();
    }
}

发布了167 篇原创文章 · 获赞 3 · 访问量 3422

猜你喜欢

转载自blog.csdn.net/qq_43515011/article/details/104297997