Delta-wave
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8736 Accepted Submission(s): 3479
题目链接
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
题意求:给出两个数M,N,求由M到N所穿过的最少边数。
解题思路链接:
解题思路链接
#include<iostream>
#include<stdio.h>
#include<set>
#include<string.h>
#include<cmath>
using namespace std;
int main()
{
long long int start,last;
while(~scanf("%lld%lld",&start,&last))
{
int level1 = (int)sqrt(start);
if(start > level1*level1) level1++;
int level2 = (int)sqrt(last);
if(last > level2*level2) level2++;
int left1 = start-(level1-1)*(level1-1);
if(left1%2) ///是奇数列
left1 = left1/2+1;
else
left1 = left1/2;
int left2 = last-(level2-1)*(level2-1);
if(left2%2)
left2 = left2/2+1;
else
left2 = left2/2;
int right1 = level1*level1-start+1;
if(right1%2)
right1 = right1/2+1;
else
right1 = right1/2;
int right2 = level2*level2-last+1;
if(right2%2)
right2 = right2/2+1;
else
right2 = right2/2;
int ans = abs(left1-left2)+abs(level1-level2)+abs(right1-right2);
printf("%d\n",ans);
}
return 0;
}
转载请注明原文地址: https://ju.6miu.com/read-22992.html