Closed Fences

A closed fence in the plane is a set of non-crossing, connected line
segments with N corners (3 < N < 200). The corners or vertices
are each distinct and are listed in counter-clockwise order in an array
{xi, yi}, i in (1..N).

Every pair of adjacent vertices defines a side of the fence. Thus
{xi yi xi+1 yi+1} is a side
of the fence for all i in (1..N). For our purposes, N+1 = 1, so that
the first and last vertices making the fence closed.

Here is a typical closed fence and a point x,y:

  1. * x3,y3
  2. x5,y5 / \
  3. x,y * * / \
  4. / \ / \
  5. / * \
  6. x6,y6* x4,y4 \
  7. | \
  8. | \
  9. x1,y1*----------------* x2,y2

Write a program which will do the following:

  • Test an ordered list of vertices {xi,yi}, i in (1..N) to see if the array is a valid fence.
  • Find the set of fence sides that a person (with no height) who is standing in the plane at position (x,y) can "see" when looking at the fence. The location x,y may fall anywhere not on the fence.

A fence side can be seen if there exists a ray that connects (x,y) and any point on the side, and the ray does not intersect any other side of the fence. A side that is parallel to the line of sight is not considered visible. In the figure, above the segments x3,y3-x4,y4; x5,y5-x6,y6; and x6-y6-x1,y1 are visible or partially visible from x,y.

PROGRAM NAME: fence4

INPUT FORMAT

Line 1: N, the number of corners in the fence
Line 2: Two space-separated integers, x and y, that are the location of the observer. Both integers will fit into 16 bits.
Line 3-N+2: A pair of space-separated integers denoting the X,Y location of the corner. The pairs are given in counterclockwise order. Both integers are no larger than 1000 in magnitude.

NOTE: I have added anNew test case #12 for this task. Let me know if you think it's wrong. Rob Be sure to include USACO in your mail subject!

SAMPLE INPUT (file fence4.in)

  1. 13
  2. 5 5
  3. 0 0
  4. 7 0
  5. 5 2
  6. 7 5
  7. 5 7
  8. 3 5
  9. 4 9
  10. 1 8
  11. 2 5
  12. 0 9
  13. -2 7
  14. 0 3
  15. -3 1

OUTPUT FORMAT

If the sequence is not a valid fence, the output is a single line containing the word "NOFENCE".

Otherwise, the output is a listing of visible fence segments, one per line, shown as four space-separated integers that represent the two corners. Express the points in the segment by showing first the point that is earlier in the input, then the point that is later. Sort the segments for output by examining the last point and showing first those points that are earlier in the input. Use the same rule on the first of the two points in case of ties.

SAMPLE OUTPUT (file fence4.out)

  1. 7
  2. 0 0 7 0
  3. 5 2 7 5
  4. 7 5 5 7
  5. 5 7 3 5
  6. -2 7 0 3
  7. 0 0 -3 1
  8. 0 3 -3 1

  1. 一道计算几何,需要细心做,特判几种情况,一种是点在某段篱笆的平行线上,这种情况是不能算看到的,这也是我第一次WA的原因。。。。T.T
    一种是将端点沿着篱笆移动一段很小的距离,这样就避免了篱笆在端点处相交的情况。
    还有就是按题目要求来排序。
    这题要是没数据的话,我肯定找不到我错在哪的 o(>﹏<)o

  1. Executing...
       Test 1: TEST OK [0.003 secs, 3508 KB]
       Test 2: TEST OK [0.003 secs, 3508 KB]
       Test 3: TEST OK [0.003 secs, 3508 KB]
       Test 4: TEST OK [0.005 secs, 3508 KB]
       Test 5: TEST OK [0.003 secs, 3508 KB]
       Test 6: TEST OK [0.014 secs, 3508 KB]
       Test 7: TEST OK [0.008 secs, 3508 KB]
       Test 8: TEST OK [0.008 secs, 3508 KB]
       Test 9: TEST OK [0.011 secs, 3508 KB]
       Test 10: TEST OK [0.008 secs, 3508 KB]
       Test 11: TEST OK [0.003 secs, 3508 KB]
       Test 12: TEST OK [0.003 secs, 3508 KB]
  2.  
  3. All tests OK.

  1. /*
  2. TASK:fence4
  3. LANG:C++
  4. */
  5.  
  6. #include <iostream>
  7. #include <vector>
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <cmath>
  11. using namespace std;
  12.  
  13. //定义点
  14. struct Point
  15. {
  16. double x;
  17. double y;
  18. };
  19. typedef Point Vector;
  20. bool operator == (const Point& p1, const Point& p2)
  21. {
  22. return fabs(p1.x - p2.x)<1e- && fabs(p1.y - p2.y)<1e-;
  23. }
  24. typedef struct Point point;
  25. Vector operator - (const Point& A, const Point& B)
  26. {
  27. return Vector{A.x-B.x, A.y-B.y};
  28. }
  29. double Cross(const Vector& A, const Vector& B)
  30. {
  31. return A.x*B.y - A.y*B.x;
  32. }
  33. //叉积
  34. double multi(point p0, point p1, point p2)
  35. {
  36. return (p1.x - p0.x )*( p2.y - p0.y )-( p2.x -p0.x )*( p1.y -p0.y );
  37. }
  38. //点积
  39. double Dot(Vector A,Vector B)
  40. {
  41. return A.x*B.x+A.y*B.y;
  42. }
  43.  
  44. //相交返回true,否则为false, 接口为两线段的端点
  45. bool isIntersected(point s1,point e1, point s2,point e2)
  46. {
  47. if(s1==s2 || s1==e2 || e1==s2 || e1==e2)
  48. {
  49. return false;
  50. }
  51. return (max(s1.x,e1.x) >=min(s2.x,e2.x)) &&
  52. (max(s2.x,e2.x)>=min(s1.x,e1.x)) &&
  53. (max(s1.y,e1.y) >=min(s2.y,e2.y)) &&
  54. (max(s2.y,e2.y) >=min(s1.y,e1.y)) &&
  55. (multi(s1,s2,e1)*multi(s1,e1,e2)>) &&
  56. (multi(s2,s1,e2)*multi(s2,e2,e1)>);
  57. }
  58.  
  59. point ps[];
  60. double x,y;
  61. int n ;
  62. bool check(int idx)
  63. {
  64. point pos=point {x,y};
  65. // 特判pos与线段idx平行的情况
  66. if(fabs(Cross(ps[idx]-pos,ps[idx+]-pos))<1e-)
  67. {
  68. return false;
  69. }
  70.  
  71. bool flag=false;
  72. for(int i=; i<n; i++)
  73. {
  74. // 将ps[idx]移动一小段距离
  75. if(i==idx)
  76. continue;
  77. double dx=(ps[idx+].x-ps[idx].x)*1e-;
  78. double dy=(ps[idx+].y-ps[idx].y)*1e-;
  79.  
  80. if(isIntersected(pos,point {ps[idx].x+dx,ps[idx].y+dy},ps[i],ps[i+]))
  81. flag=true;
  82. }
  83. if(!flag)
  84. return true;
  85. for(int i=; i<n; i++)
  86. {
  87. if(i==idx)
  88. continue;
  89.  
  90. double dx=(ps[idx].x-ps[idx+].x)*1e-;
  91. double dy=(ps[idx].y-ps[idx+].y)*1e-;
  92.  
  93. if(isIntersected(pos,point {ps[idx+].x+dx,ps[idx+].y+dy},ps[i],ps[i+]))
  94. return false;
  95. }
  96. return true;
  97. }
  98.  
  99. int main()
  100. {
  101. freopen("fence4.in","r",stdin);
  102. freopen("fence4.out","w",stdout);
  103.  
  104. cin>>n;
  105.  
  106. scanf("%lf%lf",&x,&y);
  107. for(int i=; i<n; i++)
  108. {
  109. scanf("%lf%lf",&ps[i].x,&ps[i].y);
  110. }
  111. ps[n]=ps[];
  112.  
  113. // 判断是否符合
  114. for(int i=; i<n; i++)
  115. {
  116. for(int j=; j<i-; j++)
  117. {
  118. if(isIntersected(ps[j],ps[j+],ps[i],ps[i+]))
  119. {
  120. puts("NOFENCE");
  121. exit();
  122. }
  123. }
  124. }
  125.  
  126. vector<int> ans;
  127. for(int i=; i<n; i++)
  128. {
  129. if(check(i))
  130. ans.push_back(i);
  131. }
  132.  
  133. int sz=ans.size();
  134. if(sz>= && ans[sz-]==n- && ans[sz-]==n-)
  135. swap(ans[sz-],ans[sz-]);
  136. printf("%d\n",sz);
  137. for(int i=; i<sz; i++)
  138. {
  139. if(ans[i]==n-)
  140. printf("%.0f %.0f %.0f %.0f\n",ps[ans[i]+].x,ps[ans[i]+].y,ps[ans[i]].x,ps[ans[i]].y);
  141. else
  142. printf("%.0f %.0f %.0f %.0f\n",ps[ans[i]].x,ps[ans[i]].y,ps[ans[i]+].x,ps[ans[i]+].y);
  143. }
  144.  
  145. return ;
  146. }
  1.  

USACO6.5-Closed Fences:计算几何的更多相关文章

  1. USACO 6.5 Closed Fences

    Closed Fences A closed fence in the plane is a set of non-crossing, connected line segments with N c ...

  2. USACO6.4-Electric Fences:计算几何

    Electric Fences Kolstad & Schrijvers Farmer John has decided to construct electric fences. He ha ...

  3. USACO 6.5 章节 世界上本没有龙 屠龙的人多了也便有了

    All Latin Squares 题目大意 n x n矩阵(n=2->7) 第一行1 2 3 4 5 ..N 每行每列,1-N各出现一次,求总方案数 题解 n最大为7 显然打表 写了个先数值后 ...

  4. USACO 完结的一些感想

    其实日期没有那么近啦……只是我偶尔还点进去造成的,导致我没有每一章刷完的纪念日了 但是全刷完是今天啦 讲真,题很锻炼思维能力,USACO保持着一贯猎奇的题目描述,以及尽量不用高级算法就完成的题解……例 ...

  5. 洛谷 P2735 电网 Electric Fences Label:计算几何--皮克定理

    题目描述 在本题中,格点是指横纵坐标皆为整数的点. 为了圈养他的牛,农夫约翰(Farmer John)建造了一个三角形的电网.他从原点(0,0)牵出一根通电的电线,连接格点(n,m)(0<=n& ...

  6. 2018.07.04 POJ 1265 Area(计算几何)

    Area Time Limit: 1000MS Memory Limit: 10000K Description Being well known for its highly innovative ...

  7. poj 3348:Cows(计算几何,求凸包面积)

    Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6199   Accepted: 2822 Description ...

  8. 懒加载session 无法打开 no session or session was closed 解决办法(完美解决)

           首先说明一下,hibernate的延迟加载特性(lazy).所谓的延迟加载就是当真正需要查询数据时才执行数据加载操作.因为hibernate当中支持实体对象,外键会与实体对象关联起来.如 ...

  9. 4.Android 打包时出现的Android Export aborted because fatal error were founds [closed]

    Android 程序开发完成后,如果要发布到互联网上供别人使用,就需要将自己的程序打包成Android 安装包文件(Android Package,APK),其扩展名为.apk.使用run as 也能 ...

随机推荐

  1. atitit.提升研发效率的利器---重型框架与类库的差别与设计原则

    atitit.提升研发效率的利器---重型框架与类库的差别与设计原则 1. 框架的意义---设计的复用 1 1.1. 重型框架就是it界的重武器. 1 2. 框架 VS. 库 可视化图形化 1 2.1 ...

  2. Apache http强制转为https页面访问(转)

    1 在httpd.conf文件里使下面模块生效 LoadModule rewrite_module modules/mod_rewrite.so   2 httpd.conf配置文件或者是在httpd ...

  3. 导出可执行的jar

    1.在你要导出的项目上单击右键,在弹出的右键菜单里选择:Export…选项. 2.在弹出的对话框里选择:,然后点击下边的Next; 3.在弹出的对话框里,点击选择导出后的jar存储路径以及文件名.(其 ...

  4. 改变eclipse工程中代码的层次结构

    1. 代码的层次结构 一般之代码包(package)结构 有两种:扁平结构和继承两种. 扁平结构(flat)如下图所示: 继承结构(hierarchical) 2. 如何修改: 1. 选中packag ...

  5. 【iOS解决思路】得到某个view所在的ViewController

    在一个tableViewCell中有个btn,如何得到它所在的viewcontroller,以便于push出新的viewController? 我的思路是传值,但网上有下面这种方法,分享. 跟得到某个 ...

  6. ASP.NET-FineUI开发实践-6(二)

    1.上回说到修改以前的会出现好几个5: 这是因为新增时是只新增到最后一行,所以点击选好了就跑到最后一行了,而且行号不会累积,只加到初始化的行号. 其实js里是有插入的,例子里可以插入到第一行,新增是a ...

  7. 单线程与多线程的简单示例(以Windows服务发短信为示例)

    单线程示例: public delegate void SM(); SM sm = new SM(() =>    {                    while (true)       ...

  8. mongodb查询只显示指定字段

    db.COMMODITY_COMMODITY.find( { "areaCode" : "320100" , "backCatalogId" ...

  9. VS2013默认快捷键

    目录: Global 分析 调试器上下文菜单 体系结构 调试器上下文菜单 生成 诊断中心 类视图上下文菜单 Edit 调试 编辑器上下文菜单 文件 项目和解决方案上下文菜单 帮助 重构 负载测试 解决 ...

  10. 您为这个网络适配器输入的IP地址xxx.xxx.xxx.xx已经分配给另一个适配器xxx...

    您为这个网络适配器输入的IP地址xxx.xxx.xxx.xx已经分配给另一个适配器‘xxx NIC’.... 2008年11月03日 星期一 08:51 问题现象:   在网卡的TCP/IP属性中无法 ...