计算机学院大学生程序设计竞赛(2015’12)Polygon
Polygon
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 195 Accepted Submission(s): 41
Each test case starts with one non-negative integer n (3 <= n <= 1000) giving the number of the vertices of the polygon. Then following n lines, each line contains two real numbers standing for the x and y coordinates of a vertex. The vertices are given either
in clockwise or counterclockwise order. Then four real numbers (sx, sy, tx, ty), standing for the coordinates of the two points of the line.
All real numbers are between -100000 and 100000.
4 0 0 0 1 1 1 1 0 0 0 1 1 4 0 0 0 1 1 1 1 0 0 0 1 0 9 0 0 0 2 1 1 2 2 3 1 4 2 5 1 6 2 6 0 0 1 6 1
1.414 1.000 6.000
总是望着曾经的空间发呆,那些说好不分开的朋友不在了,转身,陌路。 熟悉的,安静了, 安静的,离开了, 离开的,陌生了, 陌生的,消失了, 消失的,陌路了。
- #include<iostream>
- #include<cstring>
- #include<cstdio>
- #include<cmath>
- #include<algorithm>
- using namespace std;
- struct Point
- {
- double x;
- double y;
- } p[1001], px[10001];
- int n;
- double eps=1e-8;
- int cmp(Point a, Point b)
- {
- if(abs(a.x-b.x)<eps)
- return a.y<b.y;
- return a.x<b.x;
- }
- 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 direction(Point pi,Point pj,Point pk)
- {
- return (pk.x-pi.x)*(pj.y-pi.y)-(pj.x-pi.x)*(pk.y-pi.y);
- }
- bool on_segment(Point pi,Point pj,Point pk)
- {
- if(direction(pi, pj, pk)==0)
- {
- if(pk.x>=min(pi.x,pj.x)&&pk.x<=max(pi.x,pj.x)&&pk.y>=min(pi.y,pj.y)&&pk.y<=max(pi.y,pj.y))
- return true;
- }
- return false;
- }
- bool segments_intersect(Point p1,Point p2,Point p3,Point p4)
- {
- double d1=direction(p3,p4,p1);
- double d2=direction(p3,p4,p2);
- double d3=direction(p1,p2,p3);
- double d4=direction(p1,p2,p4);
- if(d1*d2<0&&d3*d4<0)
- return true;
- else if(d1==0&&on_segment(p3,p4,p1))
- return true;
- else if(d2==0&&on_segment(p3,p4,p2))
- return true;
- else if(d3==0&&on_segment(p1,p2,p3))
- return true;
- else if(d4==0&&on_segment(p1,p2,p4))
- return true;
- return false;
- }
- Point intersection(Point a1, Point a2, Point b1, Point b2)
- {
- Point ret = a1;
- double t = ((a1.x - b1.x) * (b1.y - b2.y) - (a1.y - b1.y) * (b1.x - b2.x))
- / ((a1.x - a2.x) * (b1.y - b2.y) - (a1.y - a2.y) * (b1.x - b2.x));
- ret.x += (a2.x - a1.x) * t;
- ret.y += (a2.y - a1.y) * t;
- return ret;
- }
- int InPolygon(Point a)
- {
- int i;
- Point b,c,d;
- b.y=a.y;
- b.x=1e15;
- int count=0;
- for(i=0; i<n; i++)
- {
- c = p[i];
- d = p[i + 1];
- if(on_segment(c,d,a))
- return 1;
- if(abs(c.y-d.y)<eps)
- continue;
- if(on_segment(a,b,c))
- {
- if(c.y>d.y)
- count++;
- }
- else if(on_segment(a,b,d))
- {
- if(d.y>c.y)
- count++;
- }
- else if(segments_intersect(a,b,c,d))
- count++;
- }
- return count%2;
- }
- bool Intersect(Point s,Point e,Point a,Point b)
- {
- return direction(e,a,s)*direction(e,b,s)<=0;
- }
- double calculate(Point s,Point e)
- {
- int i,k=0;
- double sum;
- Point a,b,temp;
- for(i=0; i<n; i++)
- {
- a=p[i];
- b=p[i+1];
- if(abs(direction(e,a,s))<eps&&abs(direction(e,b,s))<eps)
- {
- px[k++]=a;
- px[k++]=b;
- }
- else if(Intersect(s,e,a,b))
- {
- px[k++]=intersection(s,e,a,b);
- }
- }
- if(k==0)
- return 0.0;
- sort(px,px+k,cmp);
- px[k]=px[0];
- sum=0;
- for(i=0; i<k-1; i++)
- {
- a=px[i];
- b=px[i+1];
- temp.x=(a.x+b.x)/2.0;
- temp.y=(a.y+b.y)/2.0;
- if(InPolygon(temp))
- sum+=dist(a,b);
- }
- return sum;
- }
- int main()
- {
- int i;
- double sum;
- Point s,e;
- while(~scanf("%d",&n)&&n)
- {
- for(i=0; i<n; i++)
- scanf("%lf%lf",&p[i].x,&p[i].y);
- p[n]=p[0];
- scanf("%lf%lf%lf%lf",&s.x,&s.y,&e.x,&e.y);
- sum=calculate(s,e);
- printf("%.3lf\n",sum);
- }
- return 0;
- }
@执念 "@☆但求“❤”安★
下次我们做的一定会更好。。。。
为什么这次的题目是英文的。。。。QAQ...
计算机学院大学生程序设计竞赛(2015’12)Polygon的更多相关文章
- hdu 计算机学院大学生程序设计竞赛(2015’11)
搬砖 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submissi ...
- 计算机学院大学生程序设计竞赛(2015’11)1005 ACM组队安排
1005 ACM组队安排 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Pro ...
- 计算机学院大学生程序设计竞赛(2015’12) 1002 Polygon
#include<iostream> #include<cstring> #include<cstdio> #include<cmath> #inclu ...
- 计算机学院大学生程序设计竞赛(2015’12)Study Words
Study Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- 计算机学院大学生程序设计竞赛(2015’12)The Country List
The Country List Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 计算机学院大学生程序设计竞赛(2015’12) 1008 Study Words
#include<cstdio> #include<cstring> #include<map> #include<string> #include&l ...
- 计算机学院大学生程序设计竞赛(2015’12) 1009 The Magic Tower
#include<cmath> #include<cstdio> #include<cstring> #include<algorithm> using ...
- 计算机学院大学生程序设计竞赛(2015’12) 1006 01 Matrix
#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> ...
- 计算机学院大学生程序设计竞赛(2015’12) 1003 The collector’s puzzle
#include<cstdio> #include<algorithm> using namespace std; using namespace std; +; int a[ ...
随机推荐
- Apache, Nginx获得nginx代理后的真实用户Ip
Nginx 的反向代理设置 proxy_set_header X-Real-IP $remote_addr; apache可以设置日志格式将 %h替换为 %{X-Real-Ip}i 如: LogFo ...
- tomcat 6.0.44 “has failed to stop it. This is very likely to create a memory leak” 问题调查
1. 问题起因 我们项目中缓存模块某个实现采用了ehcache(2.4.3),当项目部署到tomcat中后,对tomcat做停止服务操作(点击eclipse的console红色的停止按钮,奇怪的是有小 ...
- javascript异步加载的三种解决方案
默认情况javascript是同步加载的,也就是javascript的加载时阻塞的,后面的元素要等待javascript加载完毕后才能进行再加载,对于一些意义不是很大的javascript,如果放在页 ...
- 使用jquery的小记
随便写点 1.给span这种标签赋值 不能用$("#id").val("abc"); 因为这种标签没有value属性 而应该用$("#id" ...
- c# 访问ftp
ftp从服务器上获取通信设备吐出的mr数据,该方案估计在通信行业上一个很普遍的一种方案,很奇怪为什么不把这些数据直接存储到数据库中呢,比如hadoop,反而还需要第三方搞网优的软件开发人员从ftp上读 ...
- 转:python webdriver API 之alert/confirm/prompt 处理
webdriver 中处理 JavaScript 所生成的 alert.confirm 以及 prompt 是很简单的.具体思路是使用switch_to.alert()方法定位到 alert/conf ...
- javascript 内置对象
什么是对象 javascript中的所有事物都是对象:字符串 数组 数值 函数... 每个对象都带有属性和方法 javascript允许自定义对象 自定义对象: 定义并创建对象实例 使 ...
- 通过反射封装JDBC
具体上代码我的BaseDao: public class BaseDao<T> { private Class clazz; private Properties pro=null; ...
- HDU 4834 JZP Set(数论+递推)(2014年百度之星程序设计大赛 - 初赛(第二轮))
Problem Description 一个{1, ..., n}的子集S被称为JZP集,当且仅当对于任意S中的两个数x,y,若(x+y)/2为整数,那么(x+y)/2也属于S.例如,n=3,S={1 ...
- 【crunch bang】安装firefox,删除iceweasel
首先,移除iceweasel: apt-get remove iceweasel Then, download the latest Linux build of Firefox directly f ...