ZOJ3414Trail Walk(计算几何)
FatMouse is busy organizing the coming trail walk. After the route for the trail walk has been determine, the next important task is to set the location of CPs(check point).
The route is composed by n line segments which only intersect on their endpoints. Set the starting point of the trail walk as origin, the coordinate of the endpoints are p1 p2 p3 ... pn, in the order of walking direction.
Now FatMouse wants to set m CPs on the route in such way that the walking distance between adjacent CPs are all equal. You can treat the starting point as the CP0 and the end as CPm+1.
Input
There are multiple test cases. The first line of each case contains two integer n, m(1 <= n, m <= 1000). Then n pairs of integer followed, giving the coordinate of pi.
Output
The first line of each case, output "Route case_number", Then m lines followed, the ith line contains "CPcase_num: (xi, yi)" where (xi, yi) represent the coordinate of the CPi. Always keep three number after the decimal point.
Sample Input
3 3 1 1 2 3 3 5
Sample Output
Route 1 CP1: (1.026, 1.051) CP2: (1.684, 2.368) CP3: (2.342, 3.684)
题意:比赛时看不懂。。。英语好烂。。。原点为起点(CP0),从起点出发走到终点(Pn),途径P1,p2,p3...pn,在这条路上要设置检查站。相邻的检查站之间距离要相等。原点是给出的第一个检查站位置。
难点:一开始用斜率公式dx,由于斜率k和斜边距离都是正值,所以dx永远是正值,可是dx会出现为负值的情况。并且也存在斜率不存在的情况。
所以改用相似三角形可以解决。
1 #include <cstdio> 2 #include <iostream> 3 #include <cstdlib> 4 #include <algorithm> 5 #include <ctime> 6 #include <cmath> 7 #include <string> 8 #include <cstring> 9 #include <stack> 10 #include <queue> 11 #include <list> 12 #include <vector> 13 #include <map> 14 #include <set> 15 #define LL long long 16 #define INF 0x3f3f3f3f 17 #define PI 3.1415926535897932384626 18 #define eps 1e-10 19 #define maxm 400007 20 #define maxn 1000+5 21 22 using namespace std; 23 int n,m; 24 struct Node 25 { 26 double x; 27 double y; 28 }; 29 Node node[maxn]; 30 double a[maxm]; 31 double dx[maxn]; 32 double dy[maxn]; 33 double dis[maxn]; 34 int main() 35 { 36 int cnt=0; 37 while(~scanf("%d%d",&n,&m)) 38 { 39 double sum=0; 40 node[0].x=0; 41 node[0].y=0; 42 a[0]=0; 43 for(int i=1;i<=n;i++) 44 { 45 scanf("%lf%lf",&node[i].x,&node[i].y); 46 dx[i]=node[i].x-node[i-1].x; 47 dy[i]=node[i].y-node[i-1].y; 48 dis[i]=sqrt((node[i].x-node[i-1].x)*(node[i].x-node[i-1].x)+(node[i].y-node[i-1].y)*(node[i].y-node[i-1].y)); 49 sum+=dis[i]; 50 dx[i]/=dis[i]; 51 dy[i]/=dis[i]; 52 a[i]=sum; 53 } 54 double d=sum/(m+1); 55 double t=d; 56 printf("Route %d\n",++cnt); 57 int num=0; 58 for(int i=1;i<=m;i++) 59 { 60 for(int i=1;i<=n;i++) 61 if(a[i-1]<d&&d<=a[i]) 62 { 63 64 double d1=d-a[i-1]; 65 double x1=d1*dx[i]+node[i-1].x; 66 double y1=d1*dy[i]+node[i-1].y; 67 printf("CP%d: (%.3lf, %.3lf)\n",++num,x1,y1); 68 d+=t; 69 break; 70 } 71 72 } 73 74 } 75 return 0; 76 }