HDU1030 Delta-wave(java)

Problem Description
A triangle field is numbered with successive integers in the way shown on the picture below.

这里写图片描述

The traveller needs to go from the cell with number M to the cell with number N. The traveller is able to enter the cell through cell edges only, he can not travel from cell to cell through vertices. The number of edges the traveller passes makes the length of the traveller’s route.

Write the program to determine the length of the shortest route connecting cells with numbers N and M.

Input
Input contains two integer numbers M and N in the range from 1 to 1000000000 separated with space(s).

Output
Output should contain the length of the shortest route.

Sample Input
6 12

Sample Output
3

详细的解题过程见
http://wenku.baidu.com/link?url=YPBLsB63CAVpnzB0W9qUWgDyE1xtpjm74UdE3ufMxjQkukywNF2vz_L6mkZr8IO1PYDkYwdRD7nJcaOTyknB5qkkXYSqEGfUzrg_ycK-Qxy

java代码如下

import java.util.Scanner;

public class P1030 {

    public static void main(String[] args) {
        new P1030().run();
    }

    private void run() {
        Scanner scanner = new Scanner(System.in);
        int m;
        int n;
        while (scanner.hasNextInt()) {
            m = scanner.nextInt();
            n = scanner.nextInt();
            if (m > n) {
                int temp = 0;
                temp = m;
                m = n;
                n = temp;
            }

            int rm = getRow(m);
            int lm = getColumn(m, rm);
            int rn = getRow(n);
            int ln = getColumn(n, rn);

            int out = 0;

            // 偶数时为倒三角形位置,将起点定位到上一个
            if (lm % 2 == 0) {
                rm -= 1;
                lm -= 1;
                out -= 1;
            }
            // st到en表示终点所在行,起点的控制范围
            int st = lm;
            int en = lm + (rn - rm) * 2;
            if (st <= ln && ln <= en) { // 终点在可控制范围内
                if (ln % 2 != 0)
                    out += 2 * (rn - rm);
                else {
                    out += 2 * (rn - rm) - 1;
                }
            } else { // 终点在可控制范围外
                if (ln < st) { // 可控范围的左边
                    if (st % 2 != 0)
                        out += 2 * (rn - rm);
                    else {
                        out += 2 * (rn - rm) - 1;
                    }
                    out += /* 2 * (rn - rm) + */st - ln;
                }
                if (ln > en) { // 可控范围的右边
                    if (en % 2 != 0)
                        out += 2 * (rn - rm);
                    else {
                        out += 2 * (rn - rm) - 1;
                    }
                    out += /* 2 * (rn - rm) + */ln - en;
                }
            }
            System.out.println(out);
        }
        scanner.close();
    }

    private int getRow(int v) {
        return (int) Math.ceil(Math.sqrt(v));
    }

    private int getColumn(int v, int r) {
        return v - (r - 1) * (r - 1);
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值