无限的路
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6907 Accepted Submission(s): 3517
Problem Description
甜甜从小就喜欢画图画,最近他买了一支智能画笔,由于刚刚接触,所以甜甜只会用它来画直线,于是他就在平面直角坐标系中画出如下的图形:
甜甜的好朋友蜜蜜发现上面的图还是有点规则的,于是他问甜甜:在你画的图中,我给你两个点,请你算一算连接两点的折线长度(即沿折线走的路线长度)吧。
Input
第一个数是正整数N(≤100)。代表数据的组数。
每组数据由四个非负整数组成x1,y1,x2,y2;所有的数都不会大于100。
Output
对于每组数据,输出两点(x1,y1),(x2,y2)之间的折线距离。注意输出结果精确到小数点后3位。
Sample Input
5
0 0 0 1
0 0 1 0
2 3 3 1
99 99 9 9
5 5 5 5
Sample Output
1.000
2.414
10.646
54985.047
0.000
Author
Lily
题意大概就是按照图上说的方法走,模拟+bfs就过了。之所以要bfs是因为我没有办法判断两个点是从哪一个到哪一个,只能用这种方法。
#include "cstdio"
#include "string.h"
#include "cstring"
#include "iostream"
#include "cmath"
using namespace std;
int main()
{
int ncase;
while (scanf("%d", &ncase) != EOF)
{
while (ncase--)
{
int x1, y1, x2, y2;
scanf("%d%d%d%d", &x1,&y1, &x2, &y2);
double length=0;
int xx1=x1, yy1=y1, xx2=x2, yy2=y2;
double length1 = 0, length2 = 0;
while((xx1!=x2||yy1!=y2)&&(xx2!=x1||yy2!=y1))
{
if (xx1 == 0 && yy1 == 0)
{
yy1++;
length1 += 1;
goto mark1;
}
if (yy1 == 0)
{
yy1 = xx1 + 1;
xx1 = 0;
int tempx = yy1 - 1, tempy = yy1;
length1 += sqrt(tempx*tempx + tempy*tempy);
goto mark1;
}
if (xx1 == 0)
{
xx1++;
yy1--;
length1 += 1.414213562;
goto mark1;
}
xx1++;
yy1--;
length1 += 1.414213562;
//*****************************
mark1:
if (xx2 == 0 && yy2 == 0)
{
yy2++;
length2 += 1;
goto mark2;
}
if (yy2 == 0)
{
yy2 = xx2 + 1;
xx2 = 0;
int tempx = yy2 - 1, tempy = yy2;
length2 += sqrt(tempx*tempx + tempy*tempy);
goto mark2;
}
if (xx2 == 0)
{
xx2++;
yy2--;
length2 += 1.414213562;
goto mark2;
}
xx2++;
yy2--;
length2 += 1.414213562;
mark2:
{}
if ((xx1 == x2&&yy1 == y2) || (xx2 == x1&&yy2 == y1))
break;
}
if (xx1 == x2&&yy1 == y2)
{
printf("%.3f\n", length1);
}
else
printf("%.3f\n", length2);
}
}
}