首页 技术 正文
技术 2022年11月8日
0 收藏 714 点赞 1,866 浏览 4455 个字

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 a new 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

——————————————————————题解

做的第三道计算几何

首先nofence的判定用两条线段是否相交【此处可能有图】

然后从观察者到一个点偏上一点点,偏下一点点,扫描看相交

然后求一个交点【此处可能有图】

判断交点是否在射线上

然后找一个距离观察者距离最小交点所在篱笆

 /*
LANG: C++
PROG: fence4
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#define siji(i,x,y) for(int i=(x); i <= (y) ; ++i)
#define xiaosiji(i,x,y) for(int i=(x);i < (y); ++i)
#define ivorysi
#define eps 1e-8
#define o(x) ((x)*(x))
using namespace std;
typedef long long ll;
int n;
struct vec{
double x,y;
vec operator - (const vec &rhs)const{
return (vec){x-rhs.x,y-rhs.y};
}
vec operator + (const vec &rhs)const{
return (vec){x+rhs.x,y+rhs.y};
}
vec operator * (double d)const{
return (vec){x*d,y*d};
}
vec operator / (double d)const{
return (vec){x/d,y/d};
}
double norm() const{
return x*x+y*y;
}
}pt[],observer;
struct line {
vec s,t;
}seg[];
bool visible[];
int ans;
double cross(vec a,vec b) {//求叉积
return a.x*b.y-b.x*a.y;
}
vec intersect(line a,line b) {//求交点
double s1=cross(b.s-a.s,b.t-a.s),s2=cross(b.t-a.t,b.s-a.t);
return a.s+(a.t-a.s)*s1/(s1+s2);
}
inline bool dcmp(double a,double b=) {
return fabs( a - b ) <= eps;
} bool iscross(line a,line b) {
if(cross(a.t-a.s,b.s-a.s)*cross(a.t-a.s,b.t-a.s)>= ||
cross(b.t-b.s,a.s-b.s)*cross(b.t-b.s,a.t-b.s)>=) return false;
return true;
}
void init() {
scanf("%d",&n);
scanf("%lf%lf",&observer.x,&observer.y);
siji(i,,n) {
scanf("%lf%lf",&pt[i].x,&pt[i].y);
}
siji(i,,n-) {
seg[i].s=pt[i],seg[i].t=pt[i+];
}
seg[n-].s=pt[],seg[n-].t=pt[n];
seg[n].s=pt[n-],seg[n].t=pt[n];
siji(i,,n) {
siji(j,,n) {
if(i==j) continue;
if(!iscross(seg[i],seg[j])) continue;
puts("NOFENCE");
exit();
}
}
}
void checkline(line l) {
double shortest;
int num=-;
siji(i,,n) {
if(cross(seg[i].s-l.s,l.t-l.s)*cross(seg[i].t-l.s,l.t-l.s)>=) continue;//射线只需要判定一个点
vec temp=intersect(l,seg[i])-l.s;
if(temp.x*(l.t.x-l.s.x) < || temp.y*(l.t.y-l.s.y)<) continue;//假如交点和射线上的点相乘小于0说明是不同方向
if(num==-) {
num=i;shortest=temp.norm();
}
else if(shortest>temp.norm()){
shortest=temp.norm();
num=i;
}
}
if(num!=-) visible[num]=;
}
void solve() {
init();
line l;
l.s=observer;
siji(i,,n) {
double angle=atan2(pt[i].y-l.s.y,pt[i].x-l.s.x);
l.t=l.s+(vec){cos(angle+eps),sin(angle+eps)};//偏上一点点
checkline(l);
l.t=l.s+(vec){cos(angle-eps),sin(angle-eps)};//偏下一点点
checkline(l);
}
siji(i,,n) {
if(visible[i]) ++ans;
}
printf("%d\n",ans);
siji(i,,n) {
if(visible[i]) printf("%d %d %d %d\n",
(int)seg[i].s.x,(int)seg[i].s.y,(int)seg[i].t.x,(int)seg[i].t.y);
}
}
int main(int argc, char const *argv[])
{
#ifdef ivorysi
freopen("fence4.in","r",stdin);
freopen("fence4.out","w",stdout);
#else
freopen("f1.in","r",stdin);
//freopen("f1.out","w",stdout);
#endif
solve();
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,556
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,405
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898