例题4.2 好看的一笔画 LA3263
来源:互联网 发布:虚拟软件下载 编辑:程序博客网 时间:2024/06/10 01:54
1.题目描述:点击打开链接
2.解题思路:本题利用欧拉定理解决,设顶点数,边数,面数分别是V,E,F,则V+F-E=2。因此,F=E+2-V。我们只需要求解E和V的个数即可。V的个数:除了题目中输入的点,还有两两线段相交得到的新点,由于可能出现三线共点的情况,因此最后对于顶点还要使用一下unique函数去重。对于E的个数,首先是输入的n条边(因为是一笔画构成,那么n个点会连出n条边),接下来是去重后的顶点集中,由于一个顶点在一条线段上而产生的新的边,此时我们可以枚举每一个顶点,再枚举每一条线段,如果顶点在线段上,那么个数+1.即可得到最终边的个数,套用公式即可得到答案。
3.代码:
//#pragma comment(linker, "/STACK:1024000000,1024000000")#include<iostream>#include<algorithm>#include<cassert>#include<string>#include<sstream>#include<set>#include<vector>#include<stack>#include<map>#include<queue>#include<deque>#include<cstdlib>#include<cstdio>#include<cstring>#include<cmath>#include<ctime>#include<cctype>#include<functional>using namespace std;#define me(s) memset(s,0,sizeof(s))typedef long long ll;typedef unsigned int uint;typedef unsigned long long ull;//typedef pair <int, int> P;struct Point{ double x,y; Point(double x=0,double y=0):x(x),y(y){} bool operator<(const Point&b)const { return x<b.x||(x==b.x&&y<b.y); }};typedef Point Vector;Vector operator+(Vector A,Vector B){ return Vector(A.x+B.x,A.y+B.y);}Vector operator-(Vector A,Vector B){ return Vector(A.x-B.x,A.y-B.y);}Vector operator*(Vector A,double p){ return Vector(A.x*p,A.y*p);}Vector operator/(Vector A,double p){ return Vector(A.x/p,A.y/p);}const double eps=1e-10;int dcmp(double x){ if(fabs(x)<eps)return 0; else return x<0?-1:1;}bool operator==(const Point&a,const Point&b){ return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;}double Dot(Vector A,Vector B){ return A.x*B.x+A.y*B.y;}double Length(Vector A){ return sqrt(Dot(A,A));}double Angle(Vector A,Vector B){ return acos(Dot(A,B)/Length(A)/Length(B));}double Cross(Vector A,Vector B){ return A.x*B.y-A.y*B.x;}double Area2(Point A,Point B,Point C){ return Cross(B-A,C-A);}Vector Rotate(Vector A,double rad){ return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));}Point GetLineIntersection(Point P,Vector v,Point Q,Vector w){ Vector u=P-Q; double t=Cross(w,u)/Cross(v,w); return P+v*t;}bool SegmentProperIntersection(Point a1,Point a2,Point b1,Point b2){ double c1=Cross(a2-a1,b1-a1),c2=Cross(a2-a1,b2-a1); double c3=Cross(b2-b1,a1-b1),c4=Cross(b2-b1,a2-b1); return dcmp(c1)*dcmp(c2)<0&&dcmp(c3)*dcmp(c4)<0;}bool OnSegment(Point p,Point a1,Point a2){ return dcmp(Cross(a1-p,a2-p))==0&&dcmp(Dot(a1-p,a2-p))<0;}/*=======================================================*/const int N=300+10;Point P[N],V[N*N];int main(){ int n,kase=0; while(~scanf("%d",&n)&&n) { for(int i=0;i<n;i++) { scanf("%lf%lf",&P[i].x,&P[i].y);//注意,读入的最后一个点是起点,但这里不删除它,因为枚举线段时候需要 V[i]=P[i]; } n--; //注意:由于输入的最后一个点是起点,不应该算入! int c=n,e=n; for(int i=0;i<n;i++) for(int j=i+1;j<n;j++) //注意枚举的时候j从i+1开始 if(SegmentProperIntersection(P[i],P[i+1],P[j],P[j+1]))//统计规范相交产生的交点,因为不规范的交点一定是输入的顶点 V[c++]=GetLineIntersection(P[i],P[i+1]-P[i],P[j],P[j+1]-P[j]); //得到的第一个交点自动把最后复制的起点替换掉了,即使没有多余的新点,在去重操作中它也会被去掉 sort(V,V+c); c=unique(V,V+c)-V; //由于可能存在三线共点,因此要对顶点集去重 for(int i=0;i<c;i++) for(int j=0;j<n;j++)//枚举顶点和所有线段,看顶点是否在线段上,如果是,则产生一条新的边 if(OnSegment(V[i],P[j],P[j+1]))e++; printf("Case %d: There are %d pieces.\n",++kase,e+2-c); }}
0 0
- 例题4.2 好看的一笔画 LA3263
- That Nice Euler Circuit (好看的一笔画)
- LA 3263 That Nice Euler Circuit 好看的一笔画 几何问题
- 直线交点,欧拉定理,计算几何(好看的一笔画,LA 3263)
- 一笔画的问题
- 腾讯的一笔画游戏
- 腾讯的一笔画游戏
- 腾讯的一笔画游戏
- 好看的table 样式一
- 一好看的CSS样式
- 一笔画
- LA3263 平面图的欧拉公式
- 一笔画问题的c++实现
- 判断图的连通性+一笔画问题(NYOJ 42 一笔画问题)
- 好看的
- 一笔画趣题
- 一笔画问题
- 一笔画问题
- 刀哥多线程之主队列gcd-06-main_queue
- Java面向对象
- cocos2d-x开发日志03 ——加入文字和图片
- Qt5.x windows中文乱码问题的解决方法(两种乱码问题)
- poj 3264 & poj 3468(线段树)
- 例题4.2 好看的一笔画 LA3263
- CoreGraphics对UIImage图片裁剪
- Docker基本基础知识了解
- Highways
- HDU 5391 Zball in Tina Town(数论——威尔逊定理)——BestCoder Round #51(div.2)
- 刀哥多线程同步任务作用gcd-07-sync_task
- Oracle数据库表被锁
- 技术与经济之六:现代化的陷阱
- 设计模式--浅谈