HDU1030 Delta-wave(找规律)

    xiaoxiao2021-03-25  80

    题目:

    Delta-wave

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8809    Accepted Submission(s): 3503 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   Source Ural Collegiate Programming Contest 1998   Recommend lcy   Statistic |  Submit |  Discuss |  Note 题目:

    三角形坐标系。观察整个三角形,由三个方向的直线将区域分割,横线、左斜线、右斜线。这是一个三维坐标系,三个方向的直线标定三个维度,每个区域(小三角形的标号)都可以由一组三维坐标唯一确定。

    两点间距离。在这种三角形坐标系下,重新定义两点间距离,两点之间的曼哈顿距离即为两点间的最短路径。

    找规律。仅由小三角形标号就可以确定其坐标,推倒过程依赖等差数组求和和找规律。

    层数z。前z层区域数量和 sum(z)=1+3++(2z1)=z2 ,因此, z1=a1

    z=a1+1

    左坐标。z层最大标号 z2 ,最大标号与标号a差值 z2a ,2个一组。

    l=z2a2+1

    右坐标。z-1层最大标号 (z1)2 ,标号a与上一层最大标号差值 a(z1)2 ,2个一组。

    l=a(z1)22 代码:

    #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <string> #include <iostream> #include <stack> #include <map> #include <queue> #include <vector> #include <algorithm> #define mem(a,b) memset(a,b,sizeof(a)) #define N 999999+20 #define M 200010 #define MOD 1000000000+7 #define inf 0x3f3f3f3f using namespace std; int main() { int m,n,hang_m,hang_n,r_m,r_n,l_m,l_n; while(~scanf("%d%d",&m,&n)) { hang_m=(int)ceil(sqrt(m)); hang_n=(int)ceil(sqrt(n)); r_m=(m-(hang_m-1)*(hang_m-1)-1)/2+1; r_n=(n-(hang_n-1)*(hang_n-1)-1)/2+1; l_m=(hang_m*hang_m-m)/2+1; l_n=(hang_n*hang_n-n)/2+1; int num=(int)(fabs(hang_m-hang_n)+fabs(l_m-l_n)+fabs(r_m-r_n)); printf("%d\n",num); } return 0; }

    转载请注明原文地址: https://ju.6miu.com/read-32918.html

    最新回复(0)