nyoj 226 wall (凸包 Graham算法)
2012-09-06 15:31
351 查看
Graham 算法
围墙的长途为凸包长+最近距离为半径的圆的周长
#include<stdio.h>
#include<math.h>
#include<algorithm>
#define pi 3.1415926
using namespace std;
int n,L,top;
struct point
{
int x,y;
}p[1005],s[1005],t;
double cross(point t1,point t2,point t3,point t4) //求向量t1t2和向量t3t4的叉积
{
return (t2.x-t1.x)*(t4.y-t3.y)-(t2.y-t1.y)*(t4.x-t3.x);
}
double dis(point t1,point t2) //求距离
{
double z=(t2.x-t1.x)*(t2.x-t1.x)+(t2.y-t1.y)*(t2.y-t1.y);
return sqrt(z);
}
bool cmp(point t1,point t2)
{
double z=cross(p[0],t1,p[0],t2);
return z?z>0:dis(p[0],t1)>dis(p[0],t2);
}
void findpoint() //找基点,按y从小到大,如果y相同,按x从左到右
{ int i,j=0;
t=p[0];
for(i=1;i<n;i++)
{
if(t.y>p[i].y||(t.y==p[i].y&&t.x>p[i].x))
{
j=i;
t=p[i];
}
}
t=p[0];
p[0]=p[j];
p[j]=t;
}
void scanner()
{
int i;
findpoint();
sort(p+1,p+n,cmp);
s[0]=p[0];
s[1]=p[1];
top=1;
for(i=2;i<n;i++)
{
while(cross(s[top-1],s[top],s[top],p[i])<0)
top--;
top++;
s[top]=p[i];
}
}
int main()
{
int i;
double ans;
while(~scanf("%d%d",&n,&L))
{
for(i=0;i<n;i++)
{
scanf("%d%d",&p[i].x,&p[i].y);
}
scanner();
ans=2*pi*L;
for(i=0;i<=top;i++)
{
ans+=dis(s[i],s[(i+1)%(top+1)]);
}
printf("%.0lf\n",ans);
}
return 0;
}
围墙的长途为凸包长+最近距离为半径的圆的周长
#include<stdio.h>
#include<math.h>
#include<algorithm>
#define pi 3.1415926
using namespace std;
int n,L,top;
struct point
{
int x,y;
}p[1005],s[1005],t;
double cross(point t1,point t2,point t3,point t4) //求向量t1t2和向量t3t4的叉积
{
return (t2.x-t1.x)*(t4.y-t3.y)-(t2.y-t1.y)*(t4.x-t3.x);
}
double dis(point t1,point t2) //求距离
{
double z=(t2.x-t1.x)*(t2.x-t1.x)+(t2.y-t1.y)*(t2.y-t1.y);
return sqrt(z);
}
bool cmp(point t1,point t2)
{
double z=cross(p[0],t1,p[0],t2);
return z?z>0:dis(p[0],t1)>dis(p[0],t2);
}
void findpoint() //找基点,按y从小到大,如果y相同,按x从左到右
{ int i,j=0;
t=p[0];
for(i=1;i<n;i++)
{
if(t.y>p[i].y||(t.y==p[i].y&&t.x>p[i].x))
{
j=i;
t=p[i];
}
}
t=p[0];
p[0]=p[j];
p[j]=t;
}
void scanner()
{
int i;
findpoint();
sort(p+1,p+n,cmp);
s[0]=p[0];
s[1]=p[1];
top=1;
for(i=2;i<n;i++)
{
while(cross(s[top-1],s[top],s[top],p[i])<0)
top--;
top++;
s[top]=p[i];
}
}
int main()
{
int i;
double ans;
while(~scanf("%d%d",&n,&L))
{
for(i=0;i<n;i++)
{
scanf("%d%d",&p[i].x,&p[i].y);
}
scanner();
ans=2*pi*L;
for(i=0;i<=top;i++)
{
ans+=dis(s[i],s[(i+1)%(top+1)]);
}
printf("%.0lf\n",ans);
}
return 0;
}
相关文章推荐
- 凸包的几种算法 主要Graham-Scan算法的水平序法 另加poj113 wall的解题
- nyoj 226 && hdu HDU 1348 && poj 1113 wall [凸包周长]
- POJ 1113-Wall(凸包-Graham算法)
- 【POJ1113】Wall-Graham-Scan算法求凸包
- 【计算几何初步-凸包-Graham扫描法-极角序】【HDU1348】 WALL
- 计算几何--求凸包模板--Graham算法--poj 1113
- POJ 1113 Wall(Graham求凸包周长)
- 计算几何--求凸包模板--Graham算法--poj 1113
- 凸包问题 nyoj 266 wall
- HDU 1348 Wall 标准凸包 Graham-Scan算法
- hdu 1348 Wall(凸包模板题 Graham)
- POJ 1113 Wall 凸包求边长 Graham扫描法
- 【凸包 Graham法 点集排序】poj 1113 Wall
- Graham 扫描算法求凸包,poj 2187(水题但是是我的第一个求n点集凸包模板)
- 凸包 graham 算法
- 计算几何之二维凸包:卷包裹算法、Graham Scan Algorithm、旋转卡壳算法
- Poj 1113 Wall (凸包Graham水平序)
- Graham凸包算法简介
- 【解题报告】 NYOJ 78 -- 圈水池 Andrew 凸包算法
- 凸包 graham 算法