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:

                         * x3,y3
x5,y5 / \
x,y * * / \
/ \ / \
/ * \
x6,y6* x4,y4 \
| \
| \
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)

13
5 5
0 0
7 0
5 2
7 5
5 7
3 5
4 9
1 8
2 5
0 9
-2 7
0 3
-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)

7
0 0 7 0
5 2 7 5
7 5 5 7
5 7 3 5
-2 7 0 3
0 0 -3 1
0 3 -3 1

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

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] All tests OK.

 /*
TASK:fence4
LANG:C++
*/ #include <iostream>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <cmath>
using namespace std; //定义点
struct Point
{
double x;
double y;
};
typedef Point Vector;
bool operator == (const Point& p1, const Point& p2)
{
return fabs(p1.x - p2.x)<1e- && fabs(p1.y - p2.y)<1e-;
}
typedef struct Point point;
Vector operator - (const Point& A, const Point& B)
{
return Vector{A.x-B.x, A.y-B.y};
}
double Cross(const Vector& A, const Vector& B)
{
return A.x*B.y - A.y*B.x;
}
//叉积
double multi(point p0, point p1, point p2)
{
return (p1.x - p0.x )*( p2.y - p0.y )-( p2.x -p0.x )*( p1.y -p0.y );
}
//点积
double Dot(Vector A,Vector B)
{
return A.x*B.x+A.y*B.y;
} //相交返回true,否则为false, 接口为两线段的端点
bool isIntersected(point s1,point e1, point s2,point e2)
{
if(s1==s2 || s1==e2 || e1==s2 || e1==e2)
{
return false;
}
return (max(s1.x,e1.x) >=min(s2.x,e2.x)) &&
(max(s2.x,e2.x)>=min(s1.x,e1.x)) &&
(max(s1.y,e1.y) >=min(s2.y,e2.y)) &&
(max(s2.y,e2.y) >=min(s1.y,e1.y)) &&
(multi(s1,s2,e1)*multi(s1,e1,e2)>) &&
(multi(s2,s1,e2)*multi(s2,e2,e1)>);
} point ps[];
double x,y;
int n ;
bool check(int idx)
{
point pos=point {x,y};
// 特判pos与线段idx平行的情况
if(fabs(Cross(ps[idx]-pos,ps[idx+]-pos))<1e-)
{
return false;
} bool flag=false;
for(int i=; i<n; i++)
{
// 将ps[idx]移动一小段距离
if(i==idx)
continue;
double dx=(ps[idx+].x-ps[idx].x)*1e-;
double dy=(ps[idx+].y-ps[idx].y)*1e-; if(isIntersected(pos,point {ps[idx].x+dx,ps[idx].y+dy},ps[i],ps[i+]))
flag=true;
}
if(!flag)
return true;
for(int i=; i<n; i++)
{
if(i==idx)
continue; double dx=(ps[idx].x-ps[idx+].x)*1e-;
double dy=(ps[idx].y-ps[idx+].y)*1e-; if(isIntersected(pos,point {ps[idx+].x+dx,ps[idx+].y+dy},ps[i],ps[i+]))
return false;
}
return true;
} int main()
{
freopen("fence4.in","r",stdin);
freopen("fence4.out","w",stdout); cin>>n; scanf("%lf%lf",&x,&y);
for(int i=; i<n; i++)
{
scanf("%lf%lf",&ps[i].x,&ps[i].y);
}
ps[n]=ps[]; // 判断是否符合
for(int i=; i<n; i++)
{
for(int j=; j<i-; j++)
{
if(isIntersected(ps[j],ps[j+],ps[i],ps[i+]))
{
puts("NOFENCE");
exit();
}
}
} vector<int> ans;
for(int i=; i<n; i++)
{
if(check(i))
ans.push_back(i);
} int sz=ans.size();
if(sz>= && ans[sz-]==n- && ans[sz-]==n-)
swap(ans[sz-],ans[sz-]);
printf("%d\n",sz);
for(int i=; i<sz; i++)
{
if(ans[i]==n-)
printf("%.0f %.0f %.0f %.0f\n",ps[ans[i]+].x,ps[ans[i]+].y,ps[ans[i]].x,ps[ans[i]].y);
else
printf("%.0f %.0f %.0f %.0f\n",ps[ans[i]].x,ps[ans[i]].y,ps[ans[i]+].x,ps[ans[i]+].y);
} return ;
}
												

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. 【ICIP2013】MULTI-SOURCE IMAGE AUTO-ANNOTATION

    题目:多来源图像自动标注 文中利用了不同来源的图像组内和组间的关系来增强自动标注的效果. 一方面认为,相似的图像预测的也应该是相似的,利用kNN图的关系进行组内的正则化,以此增强底层特征的联系. 另一 ...

  2. packets

    packets   时间限制(普通/Java):1000MS/10000MS     运行内存限制:65536KByte 总提交: 27            测试通过: 14 描述 A factor ...

  3. 泛泰A870刷4.4专用英文版非触摸CWM Recovery 6.0.4.8(三版通刷)

    首先声明. 发此Recovery的目的是測试能否够三版都能够启动. 而且不会出现像850 860之前出现过的卡第一屏问题! 不希望看到某些人士的过度解读!! 此Recovery能够刷第三方4.4 RO ...

  4. apache在windows上开启gzip的方法

    环境搭建好之后,默认并没有开启gzip功能.需要修改apache的httpd.conf配置文件进行开启.开启方法如下:1. httpd.conf中打开deflate_Module和headers_Mo ...

  5. qDebug 学习小结

    在qtcentre中看到有网友问这样一个问题: Why this doesn't work? qDebug() << "Test" << std::endl ...

  6. Java 编程的动态性,第 8 部分: 用代码生成取代反射--转载

    既然您已经看到了如何使用 Javassist 和 BCEL 框架来进行 classworking (请参阅 本系列以前的一组文章), 我将展示一个实际的 classworking 应用程序.这个应用程 ...

  7. linux groupmems命令

    Because users group membership is defined in two different locations, it can be difficult to find ou ...

  8. MVC的Model层中的一些便签

    由于自己重新接触MVC,所以把Model层里的一些标签给记录下来,方便自己的使用. 这些是自己目前试用过的一些,在以后的工作中我会接着补充进去新的内容

  9. activiti总结2

    根据流程号查询失败原因. activiti重试机制.齿轮节点.邮件节点.任务节点.ACT_HI_ACTINST历史表.ACT_RU_EXECUTION运行表.看图. 在Eclipse里面自己写个测试方 ...

  10. asp.net微信开发第九篇----模板消息的使用

    微信平台的模板消息,使用起来非常好,效果如下: 和平时我们微信中关注信用卡官方微信,如果消费了,信用卡官方微信就返回一个模板消息给我们告知,余额还有多少,消费了多少. 使用的步骤,我只简单介绍了怎么使 ...