The Closest Pair Problem UVA - 10245(暴力)

題目鏈接: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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章