ZOJ-3414-Trail Walk_计算几何
来源:互联网 发布:郭德纲 人品 知乎 编辑:程序博客网 时间:2024/05/19 03:22
我打代码这么狗还这么懒,以后怎么办。
题意:
给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 31 12 33 5
Sample Output
Route 1CP1: (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 1010int 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;}
0 0
- ZOJ-3414-Trail Walk_计算几何
- ZOJ 3414-Trail Walk
- ZOJ 3414 Trail Walk
- zoj计算几何
- ZOJ 1041 Transmitters(计算几何)
- ZOJ 3203 计算几何+三分
- ZOJ 3728 Collision (计算几何)
- ZOJ 3728 Collision 计算几何
- [计算几何] zoj 3728 Collision
- zoj 3728 Collision 计算几何
- zoj 1041_Transmitters_计算几何
- ZOJ 3107 Counting Squares//计算几何
- ZOJ 1721 The Doors dijstra+计算几何
- zoj 1041 计算几何 叉积运用
- ZOJ 3570 Lott's Seal 计算几何
- 【计算几何】ZOJ 2967 Colorful Rainbows
- 【计算几何】ZOJ 2976 Light Bulbs
- zoj 3203 Light Bulb(计算几何)
- slf4j+log4j、logback 配置
- Android 编程遇到的一点记录
- POJ 1915 Knight Moves 骑士遍历问题(跳马问题)
- TCP/IP学习(27)——协议初始化与简要的接收/发送流程
- ListView嵌套Gallery时click事件被Gallery捕获
- ZOJ-3414-Trail Walk_计算几何
- Tomcat配置连接池
- POJ 3264 Balanced Lineup
- iPhone开发 屏幕适配(3.5英寸和4英寸屏幕适配)
- jquery tmpl 模版
- C++栈
- 用批处理删除txt中指定的行和替换指定的行,以及添加指定的行
- 获取本地IP
- 证书与成绩单