当前位置: 首页 > 编程日记 > 正文

zoj 1010 (线段相交判断+多边形求面积)

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=10

Area

Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge

Jerry, a middle school student, addicts himself to mathematical research. Maybe the problems he has thought are really too easy to an expert. But as an amateur, especially as a 15-year-old boy, he had done very well. He is so rolling in thinking the mathematical problem that he is easily to try to solve every problem he met in a mathematical way. One day, he found a piece of paper on the desk. His younger sister, Mary, a four-year-old girl, had drawn some lines. But those lines formed a special kind of concave polygon by accident as Fig. 1 shows.


Fig. 1 The lines his sister had drawn

"Great!" he thought, "The polygon seems so regular. I had just learned how to calculate the area of triangle, rectangle and circle. I'm sure I can find out how to calculate the area of this figure." And so he did. First of all, he marked the vertexes in the polygon with their coordinates as Fig. 2 shows. And then he found the result--0.75 effortless.


Fig.2 The polygon with the coordinates of vertexes

Of course, he was not satisfied with the solution of such an easy problem. "Mmm, if there's a random polygon on the paper, then how can I calculate the area?" he asked himself. Till then, he hadn't found out the general rules on calculating the area of a random polygon. He clearly knew that the answer to this question is out of his competence. So he asked you, an erudite expert, to offer him help. The kind behavior would be highly appreciated by him.


Input

The input data consists of several figures. The first line of the input for each figure contains a single integer n, the number of vertexes in the figure. (0 <= n <= 1000).

In the following n lines, each contain a pair of real numbers, which describes the coordinates of the vertexes, (xi, yi). The figure in each test case starts from the first vertex to the second one, then from the second to the third, ���� and so on. At last, it closes from the nth vertex to the first one.

The input ends with an empty figure (n = 0). And this figure not be processed.


Output

As shown below, the output of each figure should contain the figure number and a colon followed by the area of the figure or the string "Impossible".

If the figure is a polygon, compute its area (accurate to two fractional digits). According to the input vertexes, if they cannot form a polygon (that is, one line intersects with another which shouldn't be adjoined with it, for example, in a figure with four lines, the first line intersects with the third one), just display "Impossible", indicating the figure can't be a polygon. If the amount of the vertexes is not enough to form a closed polygon, the output message should be "Impossible" either.

Print a blank line between each test cases.


Sample Input

5
0 0
0 1
0.5 0.5
1 1
1 0
4
0 0
0 1
1 0
1 1
0


Output for the Sample Input

Figure 1: 0.75

Figure 2: Impossible

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

一开始看错题意,WA了好多次,要注意与当前线段相邻接的线段不判断

主要就是第一个线段,要跳过与下一条线段的相交性,以及最后一条线段的相交性,其他线段只需要向下跳过一个线段判断相交即可

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 #include <iostream>
  5 #include <algorithm>
  6 #include <math.h>
  7 
  8 #define MAXX 1005
  9 #define eps 1e-8
 10 using namespace std;
 11 
 12 typedef struct
 13 {
 14     double x;
 15     double y;
 16 }point;
 17 
 18 typedef struct
 19 {
 20     point st;
 21     point ed;
 22 }line;
 23 
 24 point p[MAXX];
 25 line li[MAXX];
 26 
 27 double crossProduct(point a,point b,point c)
 28 {
 29     return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x);
 30 }
 31 
 32 double dist(point a,point b)
 33 {
 34     return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
 35 }
 36 
 37 bool  xy(double x,double y){ return x < y - eps; }
 38 bool  dy(double x,double y){ return x > y + eps; }
 39 bool xyd(double x,double y){ return x < y + eps; }
 40 bool dyd(double x,double y){ return x > y - eps; }
 41 bool  dd(double x,double y){ return fabs(x-y)<eps; }
 42 
 43 bool onSegment(point a,point b,point c)
 44 {
 45     double maxx=max(a.x,b.x);
 46     double maxy=max(a.y,b.y);
 47     double minx=min(a.x,b.x);
 48     double miny=min(a.y,b.y);
 49 
 50     if(dd(crossProduct(a,b,c),0.0)&&xyd(c.x,maxx)&&dyd(c.x,minx)
 51        &&xyd(c.y,maxy)&&dyd(c.y,miny))
 52         return true;
 53     return false;
 54 }
 55 
 56 bool segIntersect(point p1,point p2,point p3,point p4)
 57 {
 58     double d1=crossProduct(p3,p4,p1);
 59     double d2=crossProduct(p3,p4,p2);
 60     double d3=crossProduct(p1,p2,p3);
 61     double d4=crossProduct(p1,p2,p4);
 62 
 63     if(xy(d1*d2,0.0)&&xy(d3*d4,0.0))
 64         return true;
 65     if(dd(d1,0.0)&&onSegment(p3,p4,p1))
 66         return true;
 67     if(dd(d2,0.0)&&onSegment(p3,p4,p2))
 68         return true;
 69     if(dd(d3,0.0)&&onSegment(p1,p2,p3))
 70         return true;
 71     if(dd(d4,0.0)&&onSegment(p1,p2,p4))
 72         return true;
 73     return false;
 74 }
 75 
 76 double Area(int n)
 77 {
 78     double ans=0.0;
 79 
 80     for(int i=2; i<n; i++)
 81     {
 82         ans+=crossProduct(p[0],p[i-1],p[i]);
 83     }
 84     return fabs(ans)/2.0;
 85 }
 86 
 87 int main()
 88 {
 89     int n,m,i,j;
 90     double x,y;
 91     int cas=1;
 92     while(scanf("%d",&n)!=EOF&&n)
 93     {
 94         for(i=0; i<n; i++)
 95         {
 96             scanf("%lf%lf",&p[i].x,&p[i].y);
 97         }
 98 
 99         for(i=0; i<n-1; i++)
100         {
101             li[i].st.x=p[i].x;
102             li[i].st.y=p[i].y;
103             li[i].ed.x=p[i+1].x;
104             li[i].ed.y=p[i+1].y;
105         }
106         li[n-1].st.x=p[n-1].x;
107         li[n-1].st.y=p[n-1].y;
108         li[n-1].ed.x=p[0].x;
109         li[n-1].ed.y=p[0].y;
110         bool flag=false;
111         for(i=0; i<n; i++)
112         {
113             for(j=i+2; j<n; j++)
114             {
115                     if(i == 0 && j == n-1)continue;
116                     /*if((li[i].st.x == li[j].st.x && li[i].st.y == li[j].st.y)
117                        || li[i].st.x == li[j].ed.x && li[i].st.y == li[j].ed.y
118                        || li[i].ed.x == li[j].st.x && li[i].ed.y == li[j].st.y
119                        || li[i].ed.x == li[j].ed.x && li[i].ed.y == li[j].ed.y)
120                         continue;*/
121                     if(segIntersect(li[i].st,li[i].ed,li[j].st,li[j].ed))
122                     {
123                         flag=true;
124                         break;
125                     }
126             }
127         }
128 
129         if(flag || n<3)
130         {
131             printf("Figure %d: Impossible\n",cas++);
132         }
133         else
134         {
135             double ans=Area(n);
136             printf("Figure %d: %.2lf\n",cas++,ans);
137         }
138         printf("\n");
139     }
140     return 0;
141 }
View Code

转载于:https://www.cnblogs.com/ccccnzb/p/3903291.html

相关文章:

军用软件概算计价规范_工程造价五算:估算、概算、预算、结算、决算

估算、概算、预算、结算、决算估算即投资估算。是在决策阶段就建设项目建设总投资进行的科学估计。决策阶段又分为机会研究、项目建议书、初步可行性研究、详细可行性研究四个阶段&#xff0c;随着项目逐步的细化具体化&#xff0c;按照投资估算规程&#xff0c;可以得到不同精…

openssh配置终极一帖

一、什么是opensshOpenSSH 是 SSH &#xff08;Secure SHell&#xff09; 协议的免费开源实现。SSH协议族 可以用来进行远程控制&#xff0c; 或在计算机之间传送文件。而实现此功能的传统方式&#xff0c;如telnet(终端仿真协议)、 rcp ftp、 rlogin、rsh都是极为不安全的&a…

读书:历史 -- 海上丝绸之路

罗德里希普塔克 — 德国汉学家 海上丝路连结了古代世界贸易往来&#xff0c;见证了中华文明在人类历史中的枢纽位置。 王权集中的朝代中每一个流传后世的国家层级的决策无不彰显国家机器得强壮&#xff0c;但同样也很脆弱&#xff0c;决策者不可能时刻都能做出最为正确得选择。…

一.Linq to JSON是用来干什么的?

Linq to JSON是用来操作JSON对象的.可以用于快速查询,修改和创建JSON对象.当JSON对象内容比较复杂,而我们仅仅需要其中的一小部分数据时,可以考虑使用Linq to JSON来读取和修改部分的数据而非反序列化全部. 二.创建JSON数组和对象在进行Linq to JSON之前,首先要了解一下用于操作…

add python3.7 to path是什么意思_一起读源码:为什么 loguru 的时间 rotation 不能只精确到天...

摄影&#xff1a;产品经理猪耳朵与鹌鹑蛋做的皮蛋今天的问题来自未闻 Code 粉丝交流群&#xff1a;“loguru 每天自动生成的日志名字&#xff0c;可以只精确到日吗&#xff1f;”如下图所示&#xff1a;这里的每天自动生成日志的名字是什么意思呢&#xff1f;实际上指的就是rot…

hdu 4263(有限制的生成树)

题目链接&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid4263 思路&#xff1a;将红边和蓝边单独求一次生成树&#xff0c;求的红边最多可以加入的边数cntr&#xff0c;蓝边最多可以加入的边数cntb&#xff0c;只要k满足条件k>(n-1-cntr)&&k<cntb&#…

Synchronized的两个用法

Synchronized的作用&#xff1a; 能够保证在同一时刻最多只有一个线程执行该段代码&#xff0c;以达到保证并发安全的效果 Synchronized的两个用法&#xff1a; 1&#xff09;对象锁 包括方法锁&#xff08;默认锁对象为this当前实例对象&#xff09;和同步代码块锁&#xff08…

h5引入不同的js文件怎样让第二个js使用第一个js文件中的函数_px2rem-loader使用及注意事项...

1.安装lib-flexible.js&#xff1b; //基于vue-cli配置手淘的lib-flexible rem&#xff0c;实现移动端自适应2.安装px2rem-loader&#xff1b;//使用 webpack 的 px2rem-loader,自动将px转换为rem3.在项目入口文件main.js中引入lib-flexible&#xff1b;//&#xff08;import …

C++中的explicitkeyword

在C程序中非常少有人去使用explicitkeyword&#xff0c;不可否认&#xff0c;在平时的实践中确实非常少能用的上。再说C的功能强大&#xff0c;往往一个问题能够利用好几种C特性去解决。但略微留心一下就会发现现有的MFC库或者C标准库中的相关类声明中explicit出现的频率是非常…

Entity Framework Code First在Oracle下的伪实现

为什么要说是伪实现&#xff0c;因为还做不到类似MsSql中那样完全的功能。Oralce中的数据库还是要我们自己手动去创建的。这里&#xff0c;我们舍掉了Model First中的EDMX文件&#xff0c;自己在代码里面写模型与映射关系&#xff0c;这又有点像是Code First模型了&#xff0c;…

leetcode-206 反转链表

描述如下&#xff1a; 反转一个单链表。 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL 方法一&#xff1a;原地反转 数据结构如下 struct ListNode {int val;ListNode *next;ListNode(int x) : val(x), next(NULL) {}};ListN…

ios采用什么技术_在不锈钢技术成熟的今天,为什么汽车不采用呢?不仅仅是价格问题...

文/憨憨评车想必对于那些经常开车的人都会知道&#xff0c;我们的车子在行驶了几年之后&#xff0c;在性能方面必定是会有所下降的。然而还有一点也是非常让人头疼的&#xff0c;那就是车子的生锈问题。一旦车子的车身出现生锈情况的话&#xff0c;就会给人一种破破烂烂的感觉。…

Effective STL 为包含指针的关联容器指定比较类型

// 为包含指针的关联容器指定比较类型.cpp : 定义控制台应用程序的入口点。 //#include "stdafx.h" #include <set> #include <string> #include <iostream>using namespace std;struct StringPtrLess:public binary_function<const string*…

Android中处理崩溃异常

2019独角兽企业重金招聘Python工程师标准>>> 大家都知道&#xff0c;现在安装Android系统的手机版本和设备千差万别&#xff0c;在模拟器上运行良好的程序安装到某款手机上说不定就出现崩溃的现象&#xff0c;开发者个人不可能购买所有设备逐个调试&#xff0c;所以…

python面试基本题(你需要的)

1、冒泡排序 lis [56,12,1,8,354,10,100,34,56,7,23,456,234,-58]def sortport():for i in range(len(lis)-1):for j in range(len(lis)-1-i):if lis[j] > lis[j1]:lis[j],lis[j1] lis[j1],lis[j]return lis 2、计算x的n次方的方法 def power(x, n):s 1while n > 0:n …

leetcode-92 反转链表II

题目描述如下&#xff1a; 反转从位置 m 到 n 的链表。请使用一趟扫描完成反转。 说明: 1 ≤ m ≤ n ≤ 链表长度。 示例: 输入: 1->2->3->4->5->NULL, m 2, n 4 输出: 1->4->3->2->5->NULL 很明显这个题目是206 反转链表的进阶版 需要记…

地铁框架保护的原理_地铁屏蔽门是如何保证通讯的稳定?

地铁作为人们出行首选交通方式&#xff0c;安全可靠尤为重要&#xff0c;在复杂的地铁控制系统中&#xff0c;如何保障通讯的稳定性呢&#xff1f;本篇文章将从地铁系统中通讯单元的简单拓扑谈谈通讯防护的方案。随着我国经济的快速发展&#xff0c;地铁工程项目建设也处在快速…

HDU1548:A strange lift(Dijkstra或BFS)

题目链接 http://acm.hdu.edu.cn/showproblem.php?pid1548 题意&#xff1a;电梯每层有一个数&#xff0c;例如第n层有个数k&#xff0c; 那么这一层只能上k层或下k层&#xff0c;但是不能低于一层或高于n层&#xff0c; 给定起点与终点&#xff0c;要求出最少要按几次键 我的…

(转)C语言位运算详解

地址&#xff1a;http://www.cnblogs.com/911/archive/2008/05/20/1203477.html C语言位运算详解 作者&#xff1a;911说明&#xff1a;本文参考了http://www2.tsu.edu.cn/www/cjc/online/cyuyan/&#xff0c;算是对其的修正&#xff0c;在此将本文列为原创&#xff0c;实有抄袭…

[bzoj2300] [HAOI2011]防线修建

Description 近来A国和B国的矛盾激化&#xff0c;为了预防不测&#xff0c;A国准备修建一条长长的防线&#xff0c;当然修建防线的话&#xff0c;肯定要把需要保护的城市修在防线内部了。可是A国上层现在还犹豫不决&#xff0c;到底该把哪些城市作为保护对象呢&#xff1f;又由…

leetcode-142 环形链表II

给定一个链表&#xff0c;返回链表开始入环的第一个节点。 如果链表无环&#xff0c;则返回 null。 为了表示给定链表中的环&#xff0c;我们使用整数 pos 来表示链表尾连接到链表中的位置&#xff08;索引从 0 开始&#xff09;。 如果 pos 是 -1&#xff0c;则在该链表中没有…

1.低权限的程序向高权限的程序发消息 2.慎用setcurrentdirectory

1.低权限的程序向高权限的程序发消息 2.慎用setcurrentdirectory转载于:https://www.cnblogs.com/chunyou128/p/3921903.html

增加service_.NET Core + Kubernetes:Service

通过 .NET Core Kubernetes&#xff1a;Deployment 文章的介绍&#xff0c;我们可以通过 Deployment 控制器快速创建一组 Pod 来提供服务&#xff0c;每个 Pod 都会被分配一个集群内可见的虚拟 IP 地址&#xff0c;然后通过一个独立的 Endpoint(Pod IP ContainerPort)进行访问…

IIS配置相关问题:Framework 4.5 在IIS 7.5中运行

<system.webServer> <validation validateIntegratedModeConfiguration"false" /> <!--4.5 在IIS7.5中运行的时候--> <modules runAllManagedModulesForAllRequests"true" /> </system.webServer>转载于:https://…

[优先队列] 洛谷 P2085 最小函数值

题目描述 有n个函数&#xff0c;分别为F1,F2,...,Fn。定义Fi(x)Ai*x^2Bi*xCi (x∈N*)。给定这些Ai、Bi和Ci&#xff0c;请求出所有函数的所有函数值中最小的m个&#xff08;如有重复的要输出多个&#xff09;。 输入输出格式 输入格式&#xff1a; 输入数据&#xff1a;第一行输…

leetcode-86 分隔链表

给定一个链表和一个特定值 x&#xff0c;对链表进行分隔&#xff0c;使得所有小于 x 的节点都在大于或等于 x 的节点之前。 你应当保留两个分区中每个节点的初始相对位置。 示例: 输入: head 1->4->3->2->5->2, x 3 输出: 1->2->2->4->3->5 …

[WCF编程]1.WCF入门示例

一、WCF是什么&#xff1f; Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架&#xff0c;整合了原有的windows通讯的 .net Remoting&#xff0c;WebService&#xff0c;Socket的机制&#xff0c;并融合有Http和Ftp的相关技术&#xff0c…

ios 自动打包命令_iOS自动打包上传脚本

自从将swift2.2升级到swift3.0, 每次使用Xcode8编译都很慢&#xff0c;很是不爽&#xff0c;于是有了研究下xcodebuild命令行打包的想法&#xff0c;起初不知道用shell&#xff0c;还是用python, 在网上大概搜了一下&#xff0c;关于python的比较多点&#xff0c;于是就先学习p…

linux系统下添加新硬盘的方法详解

对于linux新手来说&#xff0c;在linux上添加新硬盘&#xff0c;是很有挑战性的一项工作。在Linux服务器上把硬盘接好&#xff0c;启动linux&#xff0c;以root登陆。 fdisk -l ## 这里是查看目前系统上有几块硬盘 Disk /dev/sda: 36.4 GB, 36401479680 bytes 255 heads, 63 s…