zoj 1010
2014-08-19 20:02
183 查看
模板题的水平。。看代码看代码
#include<stdio.h> #include<math.h> #include<algorithm> #include<iostream> using namespace std; const int maxn = 1000+10; int n; struct Point{ double x,y; Point() {} Point(double _x, double _y) { x = _x; y = _y; } Point operator - (const Point &b) const{ return Point(x-b.x, y-b.y); } }p[maxn]; double Cross(Point a, Point b) { //叉积 return a.x*b.y - a.y*b.x; } // = 0 表示,相交于端点也是认定为相交 bool SegmentProperIntersection(Point s1, Point e1, Point s2, Point e2) { double c1 = Cross(s2-s1, e1-s1), c2 = Cross(e1-s1, e2-s1), c3 = Cross(s1-s2, e2-s2), c4 = Cross(e2-s2, e1-s2); if(c1*c2 >= 0 && c3*c4 >= 0) return true; return false; } bool haveCross() { //从第 三 条线段开始, 一直到第 N-1 条线段 for(int i = 2; i < n-1; i++) { //检查 Lin(i,i+1) 与前面每一条不直接相连的线段 for(int j = 1; j < i; j++) { if(SegmentProperIntersection(p[i], p[i+1], p[j-1], p[j])) { return true; } } } //判断最后一条线段p[0]_p[n-1] //从第二条线段一直比较到第 n-2 条线段 for(int i = 1; i < n-2 ; i++) { //p[i]__p[i+1] if(SegmentProperIntersection(p[0],p[n-1],p[i],p[i+1])) { return true; } } return false; } //叉积求面积 double Area() { double area = 0; for(int i = 1; i < n-1; i++) area += Cross(p[i]-p[0], p[i+1]-p[0]); return fabs(area) / 2.0; } int main() { int test = 0; while(scanf("%d", &n) != EOF) { if(n == 0) break; double x,y; for(int i = 0; i < n; i++) { scanf("%lf%lf", &x,&y); p[i] = Point(x,y); } if(n < 3) { //如果小于三个点, 肯定不能求面积 printf("Figure %d: Impossible\n", ++test); printf("\n"); continue; } if(haveCross()) { //如果有线段相交的情况 printf("Figure %d: Impossible\n", ++test); printf("\n"); continue; } else printf("Figure %d: %.2lf", ++test,Area()); printf("\n"); } return 0; }
相关文章推荐
- ZOJ 1010 判断简单多边形+求面积
- 浙大ZOJ 1010 Area问题解决
- ZOJ 1010
- zoj 1010
- ZOJ--1010:Area(线段判交问题)
- zoj-1010 Area 多边形判别与面积
- ZOJ Problem Set - 1010
- zoj 2110 hdoj 1010 深度遍历+奇偶性剪枝
- ZOJ Problem Set - 1010
- zoj 1010 (线段相交判断+多边形求面积)
- zoj 1010 Area【线段相交问题】
- zoj 1010
- ZOJ 2110 ( HDU 1010 ) Tempter of the Bone( 比较经典的DFS) --from lanshui_Yang
- ZOJ - 1010
- zoj 1010 Area (求面积 叉积)
- ZOJ 2110(HDU 1010) Tempter of the Bone(经典剪枝-奇偶剪枝)
- ZOJ-1010 奇偶剪枝
- HDU 1010 && ZOJ 2110--Tempter of the Bone【DFS && 奇偶剪枝】
- HDU 1010 ZOJ 2110 Tempter of the Bone (DFS 奇偶剪枝)
- zoj1010——Area