poj 2253 Frogger

本文介绍了一个基于Frogger游戏的算法问题,旨在找到两块石头之间的最小跳跃距离。通过构建图并使用Kruskal算法求解最小生成树,从而确定青蛙从一块石头跳到另一块石头所需的最小跳跃范围。

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

Frogger

Description

Freddy Frog is sitting on a stone inthe middle of a lake. Suddenly he notices Fiona Frog who is sittingon another stone. He plans to visit her, but since the water isdirty and full of tourists' sunscreen, he wants to avoid swimmingand instead reach her by jumping. 
Unfortunately Fiona's stone is out of his jump range. ThereforeFreddy considers to use other stones as intermediate stops andreach her by a sequence of several smalljumps. 
To execute a given sequence of jumps, a frog's jump range obviouslymust be at least as long as the longest jump occuring in thesequence. 
The frog distance (humans also call it minimax distance) betweentwo stones therefore is defined as the minimum necessary jump rangeover all possible paths between the twostones. 

You are given the coordinates of Freddy's stone, Fiona's stone andall other stones in the lake. Your job is to compute the frogdistance between Freddy's and Fiona'sstone. 

Input

The input will contain one or moretest cases. The first line of each test case will contain thenumber of stones n (2<=n<=200). Thenext n lines each contain two integers xi,yi (0 <=xi,yi <= 1000) representing the coordinates of stone#i. Stone #1 is Freddy's stone, stone #2 is Fiona's stone, theother n-2 stones are unoccupied. There's a blank line followingeach test case. Input is terminated by a value of zero (0) forn.

Output

For each test case, print a linesaying "Scenario #x" and a line saying "Frog Distance = y" where xis replaced by the test case number (they are numbered from 1) andy is replaced by the appropriate real number, printed to threedecimals. Put a blank line after each test case, even after thelast one.

Sample Input

2

0 0

3 4

 

3

17 4

19 4

18 5

 

0

Sample Output

Scenario #1

Frog Distance = 5.000

 

Scenario #2

Frog Distance = 1.414

一直从最小的边加,当1与2在同一个集合中,当前边可以就是最小的。

另:也可以二分答案,二分这个最大值,把比这个值大的删去,看看是否能从1->2

#include<cmath>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define MAXN 1010
#define INF 100000007
using namespace std;
double g[MAXN][MAXN];
int x[MAXN],y[MAXN];
struct Edge{
    int fm,to;
    double dist;
}e[MAXN];
int f[MAXN];
int n,m;
void cal(){
    for(int i = 1;i <= n; i++)
        for(int j = i+1; j <= n;j++)
    {
        g[i][j]=sqrt((double)((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])));
        g[j][i]=g[i][j];
    }
}

void init(){
    int xx,yy,w;
    for (int i = 0; i <= n; i++)
		for (int j = 0; j <= n; j++) g[i][j]=INF;
    for (int i = 1; i <= n; i++)
		scanf("%d%d",&x[i],&y[i]);
    cal();
        m = 0;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n;j++) if (i!=j)
    {
        e[++m].dist = g[i][j];
        e[m].fm = i;
        e[m].to = j;
    }
}


int find(int x) {
    if (f[x]!=x) f[x]=find(f[x]);
    return f[x];
}
void merge(int x,int y){
    int xx=find(x);
    int yy=find(y);
    f[xx]=yy;
}

bool cmp(Edge a,Edge b){
     return a.dist<b.dist;
}

double kruskal(){
    sort(e+1,e+m+1,cmp);
    for(int i=1;i<=n;i++) f[i]=i;
    int k = 0;
    while (k++ < m-1)
    {
        int x=e[k].fm,y=e[k].to;
        merge(x,y);
        if (find(1)==find(2)) return e[k].dist;
    }
}

int main(){
    int iCase = 0;
    while (scanf("%d",&n) && n)
    {
        init();
        printf("Scenario #%d\n",++iCase);
        printf("Frog Distance = %.3f\n\n",kruskal());
    }
    return 0;
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值