Delta-wave问题:c++

来源:互联网 发布:linux下c语言删除文件 编辑:程序博客网 时间:2024/06/12 12:00

【问题描述】
一个三角形字段用下图所示的连续整数编号。

这里写图片描述
旅行者需要从m号的细胞到N号的细胞,旅行者只能通过细胞边缘进入细胞,不能通过顶点从细胞到细胞。边缘的旅行次数使得旅行者的路径的长度。
编写程序,确定连接n和m的最短路径的长度。

输入包含两个整数m和n,在1到1000000000之间用空格隔开。

【输出】
输出应包含最短路径的长度。

【输入输出样例】
in
6 12
out
3
【源程序清单】

#include<iostream>#include<cmath>#include <cstdio>#include <cstdlib>using namespace std;    int calculate_x(int n)   //该数所在层    {        return (ceil(sqrt(n)));    }    int calculate_y(int n, int x)   //该数所在右列    {        return(n - (x - 1)*(x - 1) + 1) / 2;    }    int calculate_z(int n, int x)   //该数所在左列    {        return (x*x - n) / 2 + 1;    }    int main()    {        int n, m, x1, x2, y1, y2, z1, z2, sum;        while (scanf_s("%d%d", &n, &m)!=EOF)        {               x1 = calculate_x(n);            y1 = calculate_y(n, x1);            z1 = calculate_z(n, x1);            x2= calculate_x(m);            y2=calculate_y(m, x2);            z2 = calculate_z(m, x2);            sum = abs(x1 - x2) + abs(y1 - y2) + abs(z1 - z2);  //以三维坐标形式表示出来并进行计算            printf("%d\n", sum);        }        return 0;}
原创粉丝点击