您的位置:首页 > 其它

[计算几何-凸包]pku1113-Wall

2010-01-10 09:38 453 查看
题目:没仔细看,只知道是求凸包+圆的面积- -

分析:没啥好分析的。

注:借此题复习了下凸包..发现自己忘得真厉害,尤其是用叉积判断转向..完全弄反了。叉积的时候p1和p2的先后也会影响结果。因为这个小细节郁闷了好久= =

糟糕的codes:

type
point=record x,y:longint; end;
var
n,l:longint;
a:array[0..1000] of point;
ans:array[0..1000] of longint;
function m(p0,p1,p2:point):longint;
var
temp:longint;
begin
temp:=(p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
if temp>0 then exit(1);
if temp<0 then exit(-1);
exit(0);
end;
function comp(x,y:point):boolean;
var
temp:longint;
begin
temp:=m(a[0],x,y);
if (temp>0) or (temp=0) and (sqr(x.x-a[0].x)+sqr(x.y-a[0].y)<sqr(y.x-a[0].x)+sqr(y.y-a[0].y)) then
exit(true)
else exit(false);
end;
procedure swap(var a,b:point);
var
c:point;
begin
c:=a; a:=b; b:=c;
end;
procedure qsort(l,r:longint);
var
i,j:longint;
k:point;
begin
i:=l; j:=r;
k:=a[random(r-l)+l];
while i<=j do
begin
while comp(a[i],k) do inc(i);
while comp(k,a[j]) do dec(j);
if i<=j then
begin
swap(a[i],a[j]);
inc(i); dec(j);
end;
end;
if i<r then qsort(i,r);
if l<j then qsort(l,j);
end;
procedure init;
var
i:longint;
begin
readln(n,l);
randomize;
readln(a[0].x,a[0].y);
for i:=2 to n do
begin
with a[i-1] do
readln(x,y);
if (a[i-1].y<a[0].y) or (a[i-1].y=a[0].y) and (a[i-1].x<a[0].x) then
swap(a[0],a[i-1]);
end;
qsort(1,n-1);
end;
procedure garham;
var
i,top:longint;
aa:real;
begin
for i:=1 to 3 do ans[i]:=i-1;
top:=3;
for i:=3 to n-1 do
begin
while m(a[ans[top-1]],a[ans[top]],a[i])<0 do dec(top);
inc(top);
ans[top]:=i;
end;
aa:=0;
for i:=1 to top-1 do aa:=aa+sqrt(sqr(a[ans[i]].x-a[ans[i+1]].x)+sqr(a[ans[i]].y-a[ans[i+1]].y));
aa:=aa+sqrt(sqr(a[ans[top]].x-a[ans[1]].x)+sqr(a[ans[top]].y-a[ans[1]].y));
writeln(round(aa+2*pi*l));
end;
begin
init;
garham;
end.


PS:听说cppblog的回帖比较多,想转到那,不过用pascal的可以去么= =
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: