uva10245 - The Closest Pair Proble(最近点对问题)
2013-04-13 20:52
896 查看
对于这道题,我的思路对了,但是读入的时候点的坐标必须是浮点型的数据。
还有在qsort的comp函数返回值是int型的,
所以对于double型的比较应该用关系运算符<和>来表示大小关系,
代码如下:
#include <cstdio>
#include <cstdlib>
#include <cmath>
#define M 10010
typedef struct{
double x, y;
}point;
int n;
point po[M];
int comp(const void *a, const void *b)
{
point *aa = (point*)a, *bb = (point*)b;
if(aa->x!=bb->x) return aa->x>bb->x;
return aa->y>bb->y;
}
int main ()
{
double min, x, y, tt;
while(scanf("%d",&n), n)
{
for(int i = 0; i < n; i++) scanf("%lf%lf",&po[i].x,&po[i].y);
qsort(po,n,sizeof(po[0]),comp);
min = 10000.0;
for(int i = 0; i < n; i++)
{
for(int j = i+1; j < n; j++)
{
x = fabs(po[j].x-po[i].x);
y = fabs(po[j].y-po[i].y);
if(x>=min||x>=10000.0) break;//剪枝
if(y>=min||y>=10000.0) continue;//剪枝
tt = sqrt(x*x+y*y);
if(tt>=10000.0) continue;
min = min>tt?tt:min;
}
}
if(min==10000.0) printf("INFINITY\n");
else printf("%.4lf\n",min);
}
return 0;
}
还有在qsort的comp函数返回值是int型的,
所以对于double型的比较应该用关系运算符<和>来表示大小关系,
代码如下:
#include <cstdio>
#include <cstdlib>
#include <cmath>
#define M 10010
typedef struct{
double x, y;
}point;
int n;
point po[M];
int comp(const void *a, const void *b)
{
point *aa = (point*)a, *bb = (point*)b;
if(aa->x!=bb->x) return aa->x>bb->x;
return aa->y>bb->y;
}
int main ()
{
double min, x, y, tt;
while(scanf("%d",&n), n)
{
for(int i = 0; i < n; i++) scanf("%lf%lf",&po[i].x,&po[i].y);
qsort(po,n,sizeof(po[0]),comp);
min = 10000.0;
for(int i = 0; i < n; i++)
{
for(int j = i+1; j < n; j++)
{
x = fabs(po[j].x-po[i].x);
y = fabs(po[j].y-po[i].y);
if(x>=min||x>=10000.0) break;//剪枝
if(y>=min||y>=10000.0) continue;//剪枝
tt = sqrt(x*x+y*y);
if(tt>=10000.0) continue;
min = min>tt?tt:min;
}
}
if(min==10000.0) printf("INFINITY\n");
else printf("%.4lf\n",min);
}
return 0;
}
相关文章推荐
- UVA-10245 The Closest Pair Problem(最近点对问题)
- UVa Problem 10245 The Closest Pair Problem (最近点对问题)
- The Closest Pair Problem(UVa 10245) 计算几何+最近点对问题
- UVA 10245 The Closest Pair Problem 最近点问题 分治算法
- UVA 10245 The Closest Pair Problem 最近点问题 分治算法
- UVA 10245 The Closest Pair Problem(平面最近点对)
- UVA - 10245 The Closest Pair Problem
- UVa 10245 - The Closest Pair Problem
- uva10245 The Closest Pair Problem
- UVA - 10245 The Closest Pair Problem
- UVa 10245 - The Closest Pair Problem
- 寻找最近点对问题(FCPP find the closest pair of point)
- UVA - 10245 The Closest Pair Problem 直接暴力(数据很水)
- UVA 10245 The Closest Pair Problem
- Uva10245-The Closest Pair Problem
- UVa 10245 - The Closest Pair Problem
- UVA 10245 - The Closest Pair Problem
- UVa 10245 - The Closest Pair Problem
- uva 10245 The Closest Pair Problem
- UVA 10245 The Closest Pair Problem