poj 2253 Frogger

来源:互联网 发布:网络春晚2013 编辑:程序博客网 时间:2024/05/22 01:26

先给说下题目的大意,一直青蛙坐在湖中的一个石头上,他看到另外一个青蛙在另外一个石头上,他打算从石头上跳过去去寻找另外一只青蛙。

这里有一个概念是“青蛙距离”,被定义为两块石头之间的所有路径中的最大跳跃距离的最小值。

分别给出两个青蛙所在石头的坐标以及其他石头的坐标,计算两个青蛙之间的青蛙距离。


测试数据的第一行为一个整数n,表示石头的数目。

接下来有n行,每行为两个整数,表示第i块石头的坐标。

前两行为两个青蛙的坐标。


解法:利用floyd传递闭包的特点,每个值表示两点间的边的最大值。

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespace std;typedef struct{    double x,y;} Point;int n;double Graph[300][300];double dis(const Point& a,const Point& b){    return sqrt((a.x - b.x)*(a.x - b.x)+(a.y - b.y)*(a.y - b.y));}void floyd(){    for(int k = 0 ; k < n ; k++)    {        for(int i = 0 ; i < n ; i ++)        {            for(int j = 0 ; j < n ; j++)            {                if(Graph[i][j] > max(Graph[i][k],Graph[k][j]))                {                    Graph[i][j] = max(Graph[i][k],Graph[k][j]);                }            }        }    }}int main(){    int cnt = 1;    Point point[210];    while(scanf("%d",&n) && n)    {        for(int i = 0 ; i < n ; i++)        {            scanf("%lf %lf",&point[i].x,&point[i].y);        }        for(int i = 0 ; i < n ; i++)        {            for(int j = 0 ; j < n ; j++)            {                Graph[i][j] = 0.0;            }        }        for(int i = n - 1 ; i >= 0 ; i--)        {            for(int j = i - 1 ; j >= 0 ; j--)            {                Graph[i][j] = dis(point[i],point[j]);                Graph[j][i] = Graph[i][j];            }        }        floyd();        printf("Scenario #%d\nFrog Distance = %.3lf\n\n",cnt++,Graph[0][1]);    }    return 0;}


0 0
原创粉丝点击