[HDU] [2073] Infinite Road

Problem Description

Loading...

Problem Analysis

Find the pattern of how the point coordinates change, then
each next point is the previous one plus sqrt(2.0)
The special case is when the polyline turns from going down to going up
where you add the distance between the two points

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<stdio.h>
#include<math.h>
#include<string.h>
#define gen sqrt(2.0)
double a[1000][1000];
double dis(int x1,int y1,int x2,int y2) {
	return sqrt(pow(x1-x2,2.0)+pow(y1-y2,2.0));
}
int main() {
//	freopen("input.txt","r",stdin);
//	freopen("output.txt","w",stdout);
	a[0][0]=0;
	double last=a[0][1]=1;
	int k=0;
	int j;
	for(int i=0; i<200; i++) {
		k++;
		for(j=0; j<=k; j++) {
			if(j==0&&k-j==1)
				continue;
			a[j][k-j]=last+gen;
			if(j==0)
				a[j][k-j]-=gen;
			last=a[j][k-j];
			//		printf("%d %d %lf\n",j,k-j,last);
		}
		last+=dis(k,0,0,k+1);
		//	printf("-%lf-%d-%lf-\n",last,k,dis(k-1,0,0,k));
	}
//	printf("%lf\n",a[99][99]);
	int T;
	scanf("%d",&T);
	while(T--) {
		int x1,y1,x2,y2;
		scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
		double sum1=a[x1][y1];
		double sum2=a[x2][y2];
		if(sum2>sum1) {
			printf("%.3lf\n",sum2-sum1);
		} else
			printf("%.3lf\n",sum1-sum2);
	}
	return 0;
}

Problem link: 【HDU】[2073]Endless Road

Loading...