在线编程在线课堂在线测评Anycodes在线编程

编程论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

How to use bs4??
本帖最后由 carry0987 于
Double Queue 问题描述 : The new founded Balkan Investment Group Bank (
John 问题描述 : Little John is playing very funny game
linux-command Linux命令大全搜索工具,内容包含Linux命令
Coati 是一款跨平台的代码查看工具,适用于 C/C++ 和 Java。商业软件。特性:1. 索引
系统可承载海量并发,消息收发确认机制 保障消息必达 系统采用动态智
全平台视频监控,支持安卓苹果以及pcweb,支持海康大华等主流dvr,全部源码以及文档 单聊、群聊、商
如何访问类的私有属性? 下面以 TPathData 为例,
问题:从 XE4 以来,Firemonkey 曲线绘图在移动平台不平滑的问题一直令人诟病,提交到官方的 QC 也是族繁不及备载,官方似乎有意的
操作数据库(RODBC)   odbcConnect(dsn, uid="", p
数据模式:mode函数显示任何对象的模式。常见的单个的
系统可承载海量并发,消息收发确认机制 保障消息必达 系统采用动态智
RabbitMQ与PHP(一) 项
Iease团队扩编预备中,盼望能有Ruby或者java工程师加盟。全职兼职都可以。有爱好的伴侣请与我接洽。 邮件:i
ruby 怎么设置装备摆设GTK2,求教指导下!
#include #include #include #include using namespace std; int main() {
标题如图所示: 有n盏灯,编号1~n。一开端灯都是关着的
成熟的消息收发确认机制,支持万人大群 支持开发自定义的消息sdk接口,扩展性超强 支持单/
成熟的消息收发确认机制,支持万人大群 支持开发自定义的消息sdk接口,扩展性超强 支持单/
1. 注意列表和集合的区别 set 列表表现形式: list_1
Ajax   Ajax即“Asynchronous Javascript And
大师好,我比来在做布谷鸟优
分辨提取A和B图像Harris角点,接下来须要对

[HDU杭电] HDU 1469 Video Surveillance-计算几何-[解题报告] C++

[复制链接]
发表于 2016-3-27 22:17:55 | 显示全部楼层 |阅读模式
Video Surveillance


问题描述 :
A friend of yours has taken the job of security officer at the Star-Buy Company, a famous depart- ment store. One of his tasks is to install a video surveillance system to guarantee the security of the customers (and the security of the merchandise of course) on all of the store’s countless floors. As the company has only a limited budget, there will be only one camera on every floor. But these cameras may turn around to look in every direction.The first problem is to choose where to install the camera for every floor. The only requirement is that every part of the room must be visible from there. In the following figure the left floor can be completely surveyed from the position indicated by a dot, while for the right floor, there is no such position, the given position failing to see the lower left part of the floor.

HDU 1469 Video Surveillance-计算几何-[解题报告] C++

HDU 1469 Video Surveillance-计算几何-[解题报告] C++
Before trying to install the cameras, your friend first wants to know whether there is indeed a suitable position for them. He therefore asks you to write a program that, given a ground plan, de- termines whether there is a position from which the whole floor is visible. All floor ground plans form rectangular polygons, whose edges do not intersect each other and touch each other only at the corners.
输入:
The input contains several floor descriptions. Every description starts with the number n of vertices that bound the floor (4 <= n <= 100). The next n lines contain two integers each, the x and y coordinates for the n vertices, given in clockwise order. All vertices will be distinct and at corners of the polygon. Thus the edges alternate between horizontal and vertical.A zero value for n indicates the end of the input.
输出:
For every test case first output a line with the number of the floor, as shown in the sample output. Then print a line stating “Surveillance is possible.” if there exists a position from which the entire floor can be observed, or print “Surveillance is impossible.” if there is no such position.Print a blank line after each test case.
样例输入:
  1. 4
  2. 0 0
  3. 0 1
  4. 1 1
  5. 1 0
  6. 8
  7. 0 0
  8. 0 2
  9. 1 2
  10. 1 1
  11. 2 1
  12. 2 2
  13. 3 2
  14. 3 0
  15. 0
复制代码
样例输出:
  1. Floor #1
  2. Surveillance is possible.

  3. Floor #2
  4. Surveillance is impossible.
复制代码
还是半平面交,各种模板各种套,注意点的顺序,所以就不多说了但是,这题的数据还是要提一下的,POJ 的 discuss 里面有组 80+ 行的数据,这组数据标程给出的是 possible ,但是这个多边形的核的面积是0,因为到最后,核只有一条边(也可以这样理解,一个矩形,长是大于 0 的,但是宽等于 0 ),也就是说,不能用面积来判断,否则,就一直 WA 吧……处理方法就是判断核的边数,看边数是否大于 0
代码:
  1. #include<iostream>
  2. #include<algorithm>
  3. #include<cstring>
  4. #include<cstdio>
  5. #include<cmath>
  6. using namespace std;

  7. const int N = 101;
  8. const double eps = 1e-8;
  9. const double pi = acos(-1.0);

  10. struct cpoint {//C++构造函数,默认缺省值为(0,0)
  11.     double x, y;
  12.     cpoint(double xx = 0, double yy = 0): x(xx), y(yy) {};
  13. };

  14. int dcmp(double x) {//判断参数的符号,负数返回-1,0返回0,正数返回1
  15.     if (x < -eps) return -1;
  16.         else return x > eps;
  17. }

  18. double xmult(cpoint p0, cpoint p1, cpoint p2) { // p0p1 与 p0p2 叉积
  19.     return (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y);
  20. }

  21. bool EqualPoint(cpoint a, cpoint b) {//两点相等
  22.     return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
  23. }

  24. struct cvector {//向量
  25.     cpoint s, e;
  26.     double ang, d;
  27. };

  28. //atan (double x)弧度表示的反正切
  29. //atan2(double x,double y)弧度表示的反正切,相当于atan(x/y)

  30. void setline(double x1, double y1, double x2, double y2, cvector &v) {
  31.     v.s.x = x1; v.s.y = y1;
  32.     v.e.x = x2; v.e.y = y2;
  33.     v.ang = atan2(y2 - y1, x2 - x1);
  34.         //这里的 d 代表向量(直线)和坐标轴的截距,正数表示 原点 在该向量的左边
  35.         //(这道题要求左半平面交),负号则表示 原点 在右边
  36.     if (dcmp(x1 - x2))          // x1 > x2
  37.                 v.d = (x1 * y2 - x2 * y1) / fabs(x1 - x2);
  38.     else v.d = (x1 * y2 - x2 * y1) / fabs(y1 - y2);
  39. }

  40. //判向量平行
  41. bool parallel(const cvector &a, const cvector &b) {
  42.     double u = (a.e.x - a.s.x) * (b.e.y - b.s.y) - (a.e.y - a.s.y) * (b.e.x - b.s.x);
  43.     return dcmp(u) == 0;
  44. }

  45. //求两向量(直线)交点 (两向量不能平行或重合)
  46. cpoint CrossPoint(const cvector &a, const cvector &b) {
  47.     cpoint res;
  48.     double u = xmult(a.s, a.e, b.s), v = xmult(a.e, a.s, b.e);
  49.     res.x = (b.s.x * v + b.e.x * u) / (u + v);
  50.     res.y = (b.s.y * v + b.e.y * u) / (u + v);
  51.     return res;
  52. }

  53. //半平面交排序函数[优先顺序: 1.极角 2.前面的直线在后面的左边]
  54. static bool VecCmp(const cvector &l, const cvector &r) {
  55.     if (dcmp(l.ang - r.ang)) return l.ang < r.ang;
  56.     return l.d < r.d;
  57. }

  58. cvector deq[N]; //用于计算的双端队列

  59. void HalfPanelCross(cvector vec[],int n,cpoint cp[],int &m)
  60. {
  61.         int i,tn;
  62.         sort(vec,vec+n,VecCmp);
  63.         for(tn=i=1;i<n;i++)
  64.         {
  65.                 if(dcmp(vec[i].ang-vec[i-1].ang) != 0)
  66.                         vec[tn++]=vec[i];
  67.         }n=tn;
  68.         int bot=0, top=1;
  69.         deq[0]=vec[0], deq[1]=vec[1];
  70.         for(i=2;i<tn;i++)
  71.         {
  72.                 if(parallel(deq[top],deq[top-1]) || parallel(deq[bot],deq[bot+1]))return ;
  73.                 while(bot<top && dcmp(xmult(vec[i].s,vec[i].e,CrossPoint(deq[top],deq[top-1])))<0)
  74.                         top--;
  75.                 while(bot<top && dcmp(xmult(vec[i].s,vec[i].e,CrossPoint(deq[bot],deq[bot+1])))<0)
  76.                         bot++;
  77.                 deq[++top]=vec[i];
  78.         }
  79.         while(bot<top && dcmp(xmult(deq[bot].s,deq[bot].e,CrossPoint(deq[top],deq[top-1])))<0)
  80.                 top--;
  81.         while(bot<top && dcmp(xmult(deq[top].s,deq[top].e,CrossPoint(deq[bot],deq[bot+1])))<0)
  82.                 bot++;

  83.         if(top <= bot+1)return ;

  84.         m=0;
  85.         for(i=bot;i<top;i++)
  86.                 cp[m++]=CrossPoint(deq[i],deq[i+1]);
  87.         if(bot<top+1)
  88.                 cp[m++]=CrossPoint(deq[bot],deq[top]);
  89.         m=unique(cp,cp+m,EqualPoint)-cp;
  90. }

  91. double PolygonArea(cpoint p[], int n) {
  92.     if (n < 3) return 0;
  93.     double s = p[0].y * (p[n - 1].x - p[1].x);
  94.     for (int i = 1; i < n; ++i)
  95.         s += p[i].y * (p[i - 1].x - p[(i + 1) % n].x);
  96.     return fabs(s / 2); // 顺时针方向s为负
  97. }

  98. int n,m;
  99. cvector v[N];
  100. cpoint cp[N];

  101. int main()
  102. {
  103.         int i,j,time=1;
  104.         cpoint p[N];
  105.         while(scanf("%d",&n),n)
  106.         {
  107.                 memset(cp,0,sizeof(cp));
  108.                 memset(v,0,sizeof(v));

  109.                 m=0;     //m是全局变量,注意,单纯在外部函数中初始化,有时候不靠谱

  110.                 for(i=0;i<n;i++)
  111.                         scanf("%lf%lf",&p[i].x,&p[i].y);
  112.                 p[n]=p[0];

  113.                 for(j=0,i=n;i>0;i--)
  114.                         setline(p[i].x,p[i].y,p[i-1].x,p[i-1].y,v[j++]);
  115.                 n=j;

  116.                 HalfPanelCross(v,n,cp,m);//向量(直线)集合,长度;点集,长度

  117.                 printf("Floor #%d\n",time++);

  118.                 if(!m)printf("Surveillance is impossible.\n");
  119.                 else printf("Surveillance is possible.\n");
  120.                 puts("");
  121.         }
  122.         return 0;
  123. }
复制代码
解题报告转自:http://blog.csdn.net/l04205613/article/details/6627531
在线编程(http://www.anycodes.cn)&编程论坛(http://www.52exe.cn)感谢您的支持!
回复

使用道具 举报

发布主题 上个主题 下个主题 快速回复 返回列表 官方QQ群
在线客服
客 服 中 心
群 机 器 人
网站二维码
收 起 客 服

QQ|Archiver|手机版|小黑屋|Anycodes ( ICP14002806Anycodes在线编程

GMT+8, 2018-11-22 01:12 , Processed in 1.277191 second(s), 71 queries .

Powered by Anycodes

© 2001-2013 吉林市群龙科技有限公司 Inc.

快速回复 返回顶部 返回列表