您的位置:首页 > 其它

poj 1474 Video Surveillance

2014-12-29 08:36 330 查看
Description

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.



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.

Input

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.
Output

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.
Sample
Input


4
0 0
0 1
1 1
1 0
8
0 0
0 2
1 2
1 1
2 1
2 2
3 2
3 0
0

Sample
Output


Floor #1
Surveillance is possible.

Floor #2
Surveillance is impossible.

Source

Southwestern European Regional Contest 1997
 

这题比较特殊。按我的理解貌似边都是平行坐标轴的、、于是我就想直接分四类求出约束力最强的四条边。。。于是WA了。。。还没去调,直接先用半平面交A了以后再说吧、、

发现一开始的初值赋成0了。。。坐标可以为负。。。分四类可做。。果断更快、、

 
代码基本同3335:http://blog.sina.com.cn/s/blog_7c4c33190101aeo3.html
 
还是蛮贴下吧、、
 
program pku_1474;

const eps=1e-8;

type dotsty=record

             
x,y:double;

           
end;

    
linesty=record

              
x1,y1,x2,y2,k:double;

              
sty:longint;

            
end;

var line:array[1..100] of linesty;

   
dot:array[1..100] of dotsty;

   
q:array[1..100] of longint;

   
n,cases:longint;

//============================================================================

procedure swap(x,y:longint);

var tt:linesty;

begin

  tt:=line[x]; line[x]:=line[y];
line[y]:=tt;

end;

//============================================================================

procedure qsort(l,r:longint);

var k:double;

   
i,j:longint;

begin

  k:=line[(l+r) shr 1].k; i:=l; j:=r;

  repeat

    while
line[i].k>k do inc(i);

    while
line[j].k<k do dec(j);

    if
i<=j then

    begin

     
swap(i,j);

     
inc(i); dec(j);

    end;

  until i>j;

  if l<j then qsort(l,j);

  if i<r then qsort(i,r);

end;

//============================================================================

procedure init;

var i,j:longint;

begin

  read(n);

  if n=0 then halt;

  for i:=1 to n do

   
read(line[i].x1,line[i].y1);

  for i:=1 to n-1 do
line[i].x2:=line[i+1].x1;

  for i:=1 to n-1 do
line[i].y2:=line[i+1].y1;

  line
.x2:=line[1].x1;
line
.y2:=line[1].y1;

  for i:=1 to n do

  begin

    if
(line[i].x1<line[i].x2) or

     
((line[i].x1=line[i].x2) and
(line[i].y1<line[i].y2)) then

       
line[i].sty:=1 else line[i].sty:=2;

    if
line[i].x1<>line[i].x2 then

     
line[i].k:=(line[i].y1-line[i].y2)/(line[i].x1-line[i].x2)
else

       
line[i].k:=maxlongint;

  end; i:=1; j:=n;

  repeat

   while line[i].sty=1 do
inc(i);

   while line[j].sty=2 do
dec(j);

   if i<=j then
swap(i,j);

  until i>j; qsort(1,j);
qsort(i,n);

end;

//============================================================================

function left(x,y:double; l:linesty):extended;

begin

 
left:=(l.x2-l.x1)*(y-l.y1)-(x-l.x1)*(l.y2-l.y1);

end;

//============================================================================

function cross(l1,l2:linesty):dotsty;

var s1,s2,ss,k,dx,dy:double;

begin

 
s1:=(l2.x2-l1.x1)*(l1.y2-l1.y1)-(l1.x2-l1.x1)*(l2.y2-l1.y1);

 
s2:=(l1.x2-l1.x1)*(l2.y1-l1.y1)-(l2.x1-l1.x1)*(l1.y2-l1.y1);

  ss:=s1+s2;

  if abs(ss)<eps then

  begin

   
cross.x:=maxlongint; exit;

  end;

  k:=s1/ss;

  dx:=l2.x1-l2.x2; dy:=l2.y1-l2.y2;

  cross.x:=l2.x2+dx*k;

  cross.y:=l2.y2+dy*k;

end;

//============================================================================

function half_plane:double;

var tmp:dotsty;

   
tt:double;

   
i,be,en:longint;

   
flag:boolean;

begin

  if n<3 then exit(0);

  be:=1; en:=1; q[1]:=1;

  for i:=2 to n do

  begin

    if
sqr(line[i].x1-line[i].x2)+sqr(line[i].y1-line[i].y2)<eps
then continue;

    if
abs(line[q[en]].k-line[i].k)<eps then

     
if left(line[q[en]].x1,line[q[en]].y1,line[i])>-eps
then

       
dec(en) else continue;

    while
be<en do

    begin

     
tmp:=cross(line[i],line[q[en]]);

     
if tmp.x=maxlongint then exit(-1);

     
if left(tmp.x,tmp.y,line[q[en-1]])>eps then

       
dec(en) else break;

    end;

    while
be<en do

    begin

     
if line[i].sty=1 then break;

     
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: