十二周——杭电——1002Delta-Wave

博客介绍了杭电1002Delta-Wave问题,通过使用三向坐标系统来表示格子位置,计算格子间最短距离的方法。作者分享了个人分析、程序代码以及在解决问题过程中的心得体验,描述了从困惑到理解的艰难历程。
摘要由CSDN通过智能技术生成

问题及代码:

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

个人分析:

在用三向坐标表示每一个格子的坐标后,计算两个格子之间最近的距离就变成了计算两个格子的坐标之间的差的绝对值的和

程序代码:

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    int m,n;
    int mlevel,mleft,mright,nlevel,nleft,nright;
    while(cin>>m>>n)
    {
        mlevel=1;
        nlevel=1;
        int i;//i表示的是每横行的各自的格子的总数
        for(i=1;;i=i+2)//由于水平坐标的每行是比上一行增加2两个格子数,所以此处表示格子数目的i应该是加2的
        {
            if(m-i<=0)
            {
                mright=(i-m)/2+1;//由于在计算右坐标的时候,每两条坐标线中间夹两个格子,所以而且计数是从左开始计数,于是,先计算目标格子位于哪两条右坐标之间,然后在计算位于第几个格子
                mleft=(m+1)/2;//计算目标格子的左坐标,
                break;
            }
            mlevel++;
            m=m-i;//确定好的最后的横行后,还剩余的格子数
        }
        for(i=1;;i=i+2)
        {
            if(n-i<=0)
            {
                nright=(i-n)/2+1;
                nleft=(n+1)/2;
                break;
            }
            nlevel++;
            n=n-i;
        }
        int sum=0;
        sum=abs(mlevel-nlevel)+abs(mright-nright)+abs(mleft-nleft);//有一个格子到另一个格子最近的距离就是两个格子的三个坐标差的绝对值的和
        cout<<sum<<endl;
    }
    return 0;
}


 

运行结果:

心得体会:

刚开始一点头绪都没有用。。后面大神指点的思路。。自己纠结的跟狗似的,最终搞出了这个东西,真心被虐成狗了

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值