ZOJ-3414-Trail Walk_计算几何
2014-07-17 10:45
519 查看
我打代码这么狗还这么懒,以后怎么办。
题意:
给n个点的坐标(整数),以坐标系原点为起点,依次连接各个点,给出正整数m,要求添加m个CP点(check point),把折线按长度平均分开(把起点考虑为第0个和第m+1个CP点)。
giving the coordinate of pi.
where (xi, yi) represent the coordinate of the CPi. Always keep three number after the decimal point.
计算几何里应该不难的题吧,但是由于我计算几何就是狗,而且有点新感悟就写一下。
大体过程只要按正常想法就能模拟出来,关键是公式,计算几何不要直接使用斜率的数值,但是可以通过把k写成(y1-y2)/(x1-x2)的形式表达出来,然后推出最后的式子以后再代数,这样就不会有使用斜率数值的误差了。
代码如下
题意:
给n个点的坐标(整数),以坐标系原点为起点,依次连接各个点,给出正整数m,要求添加m个CP点(check point),把折线按长度平均分开(把起点考虑为第0个和第m+1个CP点)。
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)
计算几何里应该不难的题吧,但是由于我计算几何就是狗,而且有点新感悟就写一下。
大体过程只要按正常想法就能模拟出来,关键是公式,计算几何不要直接使用斜率的数值,但是可以通过把k写成(y1-y2)/(x1-x2)的形式表达出来,然后推出最后的式子以后再代数,这样就不会有使用斜率数值的误差了。
代码如下
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> using namespace std; #define mxn 1010 int n,m; double len; bool dcmp(double a,double b){ return fabs(a-b)<0.001; } struct point{ double x,y; point(double _x,double _y){x=_x,y=_y;} point(){}; }p[mxn]; struct ret{ int p_id; double usd; ret(int a,double b){p_id=a,usd=b;} }; double dist(point a,point b){ return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } double deltaX(int in,double l){ return l*(p[in+1].x-p[in].x)/sqrt((p[in+1].y-p[in].y)*(p[in+1].y-p[in].y)+(p[in+1].x-p[in].x)*(p[in+1].x-p[in].x)); } double deltaY(int in,double l){ return l*(p[in+1].y-p[in].y)/sqrt((p[in+1].y-p[in].y)*(p[in+1].y-p[in].y)+(p[in+1].x-p[in].x)*(p[in+1].x-p[in].x)); } ret find(ret in,double tgt,int cp_id){ double tem=dist(p[in.p_id],p[in.p_id+1]); if(tem-in.usd<=tgt||dcmp(tem-in.usd,tgt)) return find(ret(in.p_id+1,0.0),tgt-tem+in.usd,cp_id); printf("CP%d: ",cp_id); double delta_x=deltaX(in.p_id,tgt+in.usd); double delta_y=deltaY(in.p_id,tgt+in.usd); printf("(%.3lf, %.3lf)\n",p[in.p_id].x+delta_x,p[in.p_id].y+delta_y); return ret(in.p_id,in.usd+tgt); } int main(){ int cs=1; while(scanf("%d%d",&n,&m)!=EOF){ double sum=0; p[0]=point(0.0,0.0); for(int i=1;i<=n;++i){ scanf("%lf%lf",&p[i].x,&p[i].y); sum+=dist(p[i-1],p[i]); } len=sum/(m+1); ret start(0,0.0); printf("Route %d\n",cs++); for(int i=0;i<m;++i){ ret tem=find(start,len,i+1); start=tem; } } return 0; }
相关文章推荐
- ZOJ 3414-Trail Walk
- zoj 1041 计算几何 叉积运用
- ACM-计算几何之Quoit Design——hdu1007 zoj2107
- I Think I Need a Houseboat ZOJ(几何计算)
- zoj计算几何
- ZOJ 3203 计算几何+三分
- ZOJ 3598 Spherical Triangle(计算几何 球面三角形内角和)
- 【计算几何】ZOJ 2967 Colorful Rainbows
- ZOJ - 3574 Under Attack II(计算几何 归并排序)
- ZOJ 2419-- Triangle-凸包+旋转卡壳求最大面积三角形(计算几何)
- ZOJ - 3806 Incircle and Circumcircle(计算几何+二分)
- ZOJ 3203 Light Bulb (三分+计算几何)
- ZOJ-3720 Magnet Darts 计算几何,概率
- ZOJ 3537 Cake 计算几何+区间dp
- ZOJ 1608 Two Circles and a Rectangle(简单计算几何)
- [计算几何] zoj 3728 Collision
- zoj 3720 计算几何,期望
- zoj 2318 Get Out! 计算几何 spfa判负环
- 计算几何基础与应用:HDU 1348&&ZOJ 1648&&POJ 2398&&ZOJ 1010
- [ZOJ 2287][Vjudge 40177] Shuttlecock [计算几何]