求多边形核的存在性,过了这题但是过不了另一题的,不知道是模板的问题还是什么,但是这个模板还是可以过绝大部分的题的。。。

  1. #pragma warning(disable:4996)
  2. #include <iostream>
  3. #include <cstring>
  4. #include <cstdio>
  5. #include <vector>
  6. #include <cmath>
  7. #include <string>
  8. #include <algorithm>
  9. using namespace std;
  10.  
  11. #define maxn 2500
  12. #define eps 1e-7
  13.  
  14. int n;
  15.  
  16. int dcmp(double x){
  17. return x<-eps ? -1 : x>eps;
  18. }
  19.  
  20. struct Point
  21. {
  22. double x, y;
  23. Point(){}
  24. Point(double _x, double _y) :x(_x), y(_y){}
  25. Point operator + (const Point &b) const{
  26. return Point(x + b.x, y + b.y);
  27. }
  28. Point operator - (const Point &b) const{
  29. return Point(x - b.x, y - b.y);
  30. }
  31. Point operator *(double d) const{
  32. return Point(x*d, y*d);
  33. }
  34. Point operator /(double d) const{
  35. return Point(x / d, y / d);
  36. }
  37. double det(const Point &b) const{
  38. return x*b.y - y*b.x;
  39. }
  40. double dot(const Point &b) const{
  41. return x*b.x + y*b.y;
  42. }
  43. Point rot90(){
  44. return Point(-y, x);
  45. }
  46. Point norm(){
  47. double len = sqrt(this->dot(*this));
  48. return Point(x, y) / len;
  49. }
  50. void read(){
  51. scanf("%lf%lf", &x, &y);
  52. }
  53. };
  54.  
  55. #define cross(p1,p2,p3) ((p2.x-p1.x)*(p3.y-p1.y)-(p3.x-p1.x)*(p2.y-p1.y))
  56. #define crossOp(p1,p2,p3) (dcmp(cross(p1,p2,p3)))
  57.  
  58. Point isSS(Point p1, Point p2, Point q1, Point q2){
  59. double a1 = cross(q1, q2, p1), a2 = -cross(q1, q2, p2);
  60. return (p1*a2 + p2*a1) / (a1 + a2);
  61. }
  62.  
  63. struct Border
  64. {
  65. Point p1, p2;
  66. double alpha;
  67. void setAlpha(){
  68. alpha = atan2(p2.y - p1.y, p2.x - p1.x);
  69. }
  70. };
  71.  
  72. bool operator < (const Border &a, const Border &b) {
  73. int c = dcmp(a.alpha - b.alpha);
  74. if (c != 0) {
  75. return c > 0;
  76. }
  77. else {
  78. return crossOp(b.p1, b.p2, a.p1) > 0;
  79. }
  80. }
  81.  
  82. bool operator == (const Border &a, const Border &b){
  83. return dcmp(a.alpha - b.alpha) == 0;
  84. }
  85.  
  86. Point isBorder(const Border &a, const Border &b){
  87. return isSS(a.p1, a.p2, b.p1, b.p2);
  88. }
  89.  
  90. Border border[maxn];
  91. Border que[maxn];
  92. int qh, qt;
  93. // check函数判断的是新加的半平面和由a,b两个半平面产生的交点的方向,若在半平面的左侧返回True
  94. bool check(const Border &a, const Border &b, const Border &me){
  95. Point is = isBorder(a, b);
  96. return crossOp(me.p1, me.p2, is) >= 0;
  97. }
  98.  
  99. bool isParellel(const Border &a, const Border &b){
  100. return dcmp((a.p2 - a.p1).det(b.p2 - b.p1)) == 0;
  101. }
  102.  
  103. bool convexIntersection()
  104. {
  105. qh = qt = 0;
  106. sort(border, border + n);
  107. n = unique(border, border + n) - border;
  108. for (int i = 0; i < n; i++){
  109. Border cur = border[i];
  110. while (qh + 1 < qt&&!check(que[qt - 2], que[qt - 1], cur)) --qt;
  111. while (qh + 1 < qt&&!check(que[qh], que[qh + 1], cur)) ++qh;
  112. que[qt++] = cur;
  113. }
  114. while (qh + 1 < qt&&!check(que[qt - 2], que[qt - 1], que[qh])) --qt;
  115. while (qh + 1 < qt&&!check(que[qh], que[qh + 1], que[qt - 1])) ++qh;
  116. return qt - qh > 2;
  117. }
  118.  
  119. Point ps[maxn];
  120.  
  121. int main()
  122. {
  123. int ca = 0;
  124. while (cin >> n&&n)
  125. {
  126. for (int i = 0; i < n; i++){
  127. ps[i].read();
  128. }
  129. ps[n] = ps[0];
  130. for (int i = 0; i < n; i++){
  131. border[i].p1 = ps[i + 1];
  132. border[i].p2 = ps[i];
  133. border[i].setAlpha();
  134. }
  135. printf("Floor #%d\n", ++ca);
  136. if (convexIntersection()) {
  137. puts("Surveillance is possible.");
  138. }
  139. else puts("Surveillance is impossible.");
  140. puts("");
  141. }
  142. return 0;
  143. }

POJ1474 Video Surveillance(半平面交)的更多相关文章

  1. POJ 1474 Video Surveillance 半平面交/多边形核是否存在

    http://poj.org/problem?id=1474 解法同POJ 1279 A一送一 缺点是还是O(n^2) ...nlogn的过几天补上... /********************* ...

  2. POJ 1474 Video Surveillance(半平面交)

    题目链接 2Y,模版抄错了一点. #include <cstdio> #include <cstring> #include <string> #include & ...

  3. poj1474Video Surveillance(半平面交)

    链接 半平面交的模板题,判断有没有核.: 注意一下最后的核可能为一条线,面积也是为0的,但却是有的. #include<iostream> #include <stdio.h> ...

  4. poj1474 Video Surveillance

    题意:求多边形的内核,即:在多边形内部找到某个点,使得从这个点能不受阻碍地看到多边形的所有位置. 只要能看到所有的边,就能看到所有的位置.那么如果我们能够在多边形的内部的点x看到某条边AB,这个点x一 ...

  5. POJ 半平面交 模板题 三枚

    给出三个半平面交的裸题. 不会的上百度上谷(gu)歌(gou)一下. 毕竟学长的语文是体育老师教的.(卡格玩笑,别当真.) 这种东西明白就好,代码可以当模板. //poj1474 Video Surv ...

  6. poj 1474 Video Surveillance (半平面交)

    链接:http://poj.org/problem?id=1474 Video Surveillance Time Limit: 1000MS   Memory Limit: 10000K Total ...

  7. poj 1474 Video Surveillance 【半平面交】

    半平面交求多边形的核,注意边是顺时针给出的 //卡精致死于是换(?)了一种求半平面交的方法-- #include<iostream> #include<cstdio> #inc ...

  8. POJ3335 POJ3130 POJ1474 [半平面交]

    终于写出自己的半平面交模板了....... 加入交点的地方用了直线线段相交判定 三个题一样,能从任何地方看到就是多边形的内核 只不过一个顺时针一个逆时针(给出一个多边形的两种方式啦),反正那个CutP ...

  9. POJ1474:Video Surveillance——题解

    http://poj.org/problem?id=1474 题目大意:给按照顺时针序的多边形顶点,问其是否有内核. —————————————————————————————— (和上道题目一模一样 ...

随机推荐

  1. Python核心编程--学习笔记--8--条件与循环

    本章讲述if.while.for以及与他们搭配的else.elif.break.continue.pass等语句. 1 if语句 语法:三部分——关键字if.条件表达式.代码块.(记住冒号) if c ...

  2. Python可以做什么?

    Python是一个优秀的程序语言,PYthon的应用角色几乎是无限的,你可以在任何场合应用Python. 1 系统编程 Python提供对系统管理的内部接口,可以搜索文件和目录,可以运行其他程序 Py ...

  3. WebService到底是什么?(转)

    一.序言 大家或多或少都听过WebService(Web服务),有一段时间很多计算机期刊.书籍和网站都大肆的提及和宣传WebService技术,其中不乏很多吹嘘和做广告的成分.但是不得不承认的是Web ...

  4. C,C++容易被忽略的问题

    1.字符串数组,字符串指针可以直接输出 ]="I am a student"; cout<<s2<<endl; char *p="I am a s ...

  5. 行转列求和:不加 in 条件,sum的数据会不会准确?

    我的习惯写法,担心不加 in 条件 ,统计结果会包含其他的数据 SELECT ZWKMYE_KJND as 年度,ZWKMYE_KJQJ as 月份,ZWKMYE_DWBH as 单位, ' then ...

  6. Android--ViewPager的无限轮播

    ViewPage_RadioButton实现带小圆点的无限轮播,效果还能凑合着用. 1.在ViewPage的监听里面这样处理 @Override public void onPageSelected( ...

  7. ListView与GridView异步加载图片

    原理很简单,主要是用到了回调方法,下面是异步加载图片的类 <span style="font-size:16px;">package com.xxx.xxx; impo ...

  8. 微软云平台媒体服务实践系列 2- 使用动态封装为iOS, Android , Windows 等多平台提供视频点播(VoD)方案

    文章微软云平台媒体服务实践系列 1- 使用静态封装为iOS, Android 设备实现点播(VoD)方案  介绍了如何针对少数iOS, Android 客户端的场景,出于节约成本的目的使用媒体服务的静 ...

  9. TcpClient 错误"不能做任何连接,因为目标机器积极地拒绝它" 的解决

    TcpClient 错误"不能做任何连接,因为目标机器积极地拒绝它" 的解决 //以下是tcpclient服务器端的监听程序,假设服务器端和客户端在同一台机器上,//为了使客户端可 ...

  10. 5 给我们的c#程序添加注释

    注释是你的程序中的一个重要部分.在程序中添加注释是用来告诉你和其他人你的程序是做什么用的,你的思路是怎样的.注释可以用你熟悉的中文进行添加. 当你想暂时把你程序中的某些语句去掉的时候,不需要把他们删除 ...