首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

hdu 1348 wa?该如何解决

2012-12-24 
hdu 1348 wa?//做这个凸包的问题太郁闷了,总是wa,却找不出错误来,还请各位前辈花时间帮忙看一下,到底哪里

hdu 1348 wa?
//做这个凸包的问题太郁闷了,总是wa,却找不出错误来,还请各位前辈花时间帮忙看一下,到底哪里错了。
//代码如下:

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#define MAX 1001
using namespace std;
const double PI=acos(-1);
typedef struct Point1{
double x,y;
}POINT;
typedef struct Point2{
double x,y,angle;
}SPOINT;
double Distance(POINT &a,POINT &b);
void  RecoverCoordinates(POINT answer[],int count,POINT CopyPoint);
void Solve(POINT S[],POINT answer[],int n,int &sp);//
void swap(POINT &a,POINT &b);
bool cmp(const SPOINT &a,const SPOINT &b);
int main(){
int i,t,n,L,sp;
POINT S[MAX+1],answer[MAX+1];
double Mindistance=0;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&L);
for(i=0;i<n;++i){
scanf("%lf%lf",&S[i].x,&S[i].y);
}
Solve(S,answer,n,sp);
Mindistance=0;
for(i=0;i<sp;++i){
Mindistance+=Distance(answer[i],answer[i+1]);
}
Mindistance+=2*PI*L;
printf("%.0lf\n",Mindistance);
if(t>1)
printf("\n");
}
return 0;
}
void Solve(POINT S[],POINT answer[],int n,int &sp){
int i,k;
SPOINT T[MAX+1];
double judge;
//choose the smallest Y 
for(i=1;i<n;++i){
if(S[i].y<S[0].y || (S[i].y==S[0].y && S[i].x>S[0].x))
swap(S[i],S[0]);
}
//coordinate transformation
for(i=1;i<n;++i){
T[i-1].x=S[i].x-S[0].x;
T[i-1].y=S[i].y-S[0].y;
T[i-1].angle=atan2(T[i-1].y,T[i-1].x);
}
//sort by angle
sort(T,T+n-1,cmp);
//initialize the answer array
answer[0].x=T[n-2].x;  
answer[0].y=T[n-2].y;
answer[1].x=0;
answer[1].y=0;
sp=1;
k=0;
while(k<n-1){
judge=answer[sp-1].x*answer[sp].y+answer[sp].x*T[k].y+T[k].x*answer[sp-1].y
     -answer[sp-1].y*answer[sp].x-answer[sp].y*T[k].x-T[k].y*answer[sp-1].x;
  if(judge>=0){
++sp;
answer[sp].x=T[k].x;
answer[sp].y=T[k].y;
++k;  
  }
  else
  --sp;
}
RecoverCoordinates(answer,sp,S[0]);
}
void swap(POINT &a,POINT &b){
POINT tmp;
tmp=a;
a=b;
b=tmp;
}
bool cmp(const SPOINT &a,const SPOINT &b){
return a.angle<b.angle?true:false;
}
double Distance(POINT&a,POINT &b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
void RecoverCoordinates(POINT answer[],int count,POINT CopyPoint){
for(int i=0;i<=count;++i){
answer[i].x+=CopyPoint.x;
answer[i].y+=CopyPoint.y;
}
}

[解决办法]
if(t>1)
printf("\n");
确定是t>1?

热点排行