The Closest Pair Problem UVA - 10245(暴力)

本文介绍了一道经典的计算几何题目——寻找二维平面上距离最近的两点。通过使用C++实现暴力求解的方法,并详细展示了完整的代码实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:https://vjudge.net/problem/UVA-10245

题意:n个点,求距离最近的两点之间的距离,若没有距离小于10000,则输出INFINITY。

思路:直接暴力

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstdlib>
#include<sstream>
#include<deque>
#include<stack>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-9;
const int  maxn = 10000 + 20;
const int  maxt = 300 + 10;
const int mod = 10;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, -1, 1};
const int Dis[] = {-1, 1, -5, 5};
const double inf = 0x3f3f3f3f;
const int MOD = 1000;
int n, m, k;
struct node{
    double x, y;
    node(double x = 0, double y = 0) : x(x), y(y){}
    bool operator < (const node &no) const{
        if(x != no.x + eps) return x < no.x;
        return y < no.y;
    }
}no[maxn];
double solve(node a, node b){
    return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
int main(){
    while(~scanf("%d", &n) && n){
        memset(no, 0, sizeof no);
        for(int i = 0; i < n; ++i){
            scanf("%lf%lf", &no[i].x, &no[i].y);
        }
        sort(no, no + n);
        double ans = inf, dis;
        for(int i = 0; i < n; ++i){
            for(int j = i + 1; j < n; ++j){
                dis = solve(no[i], no[j]);
                if(dis < 10000.0 + eps && dis < ans + eps){
                    ans = dis;
                }
            }
        }
        if(ans == inf) printf("INFINITY\n");
        else printf("%.4lf\n", ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值