首页 > 学院 > 开发设计 > 正文

POJ 2253 Frogger(Dijkstra变形)

2019-11-06 06:23:51
字体:
来源:转载
供稿:网友

POJ 2253 Frogger

Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to visit her, but since the water is dirty and full of tourists' sunscreen, he wants to avoid swimming and instead reach her by jumping. Unfortunately Fiona's stone is out of his jump range. Therefore Freddy considers to use other stones as intermediate stops and reach her by a sequence of several small jumps.To execute a given sequence of jumps, a frog's jump range obviously must be at least as long as the longest jump occuring in the sequence.The frog distance (humans also call it minimax distance) between two stones therefore is defined as the minimum necessary jump range over all possible paths between the two stones.You are given the coordinates of Freddy's stone, Fiona's stone and all other stones in the lake. Your job is to compute the frog distance between Freddy's and Fiona's stone.InputThe input will contain one or more test cases. The first line of each test case will contain the number of stones n (2<=n<=200). The next 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, the other n-2 stones are unoccupied. There's a blank line following each test case. Input is terminated by a value of zero (0) for n.OutputFor each test case, print a line saying "Scenario #x" and a line saying "Frog Distance = y" where x is replaced by the test case number (they are numbered from 1) and y is replaced by the appropriate real number, printed to three decimals. Put a blank line after each test case, even after the last one. Sample Input20 03 4317 419 418 50Sample OutputScenario #1Frog Distance = 5.000Scenario #2Frog Distance = 1.414

求的是路径中两相邻点最大距离的最小值。

wa到怀疑人生,一度以为是自己算法错误,于是去验证自己的方法,越验证越觉得没有问题。最后发现是看错了题目!!!

第二只青蛙的位置是第2组输入的数据,一直以为是第N组……比赛的时候如果犯这种错误,那是要悔断肠子的啊TAT

一定要仔细审题!!

回到正题,个人觉得这道题目还是挺有意思的。通过借鉴Dijkstra的思路可以顺利做出,所以要好好理解Dijkstra,这里的dis数组存储的是从起点到各个点的路径中两相邻点最大距离的最小值,和Dijkstra一样,每次找到dis值最小的未访问点,然后以这个点为中心来更新dis的数值,新的dis[i]值有三种可能:

1)中心的dis值

2)中心到i点的距离

3)维持初始值。

#include<cstdio>#include<cstring>#include<algorithm>#include<math.h>#define maxn 210using namespace std;int N;typedef struct{    int x,y;}node;node stone[maxn];bool vis[maxn];double dis[maxn];double maps[maxn][maxn];int findmin(){    int i,flag,mins=10086;    for(i=1;i<=N;i++){        if(!vis[i]&&dis[i]<=mins){            flag=i;            mins=dis[i];        }    }    return flag;}int ends(){    int i;    for(i=1;i<=N;i++){        if(vis[i]==0){            return 0;        }    }    return 1;}int main(){    int i,j,k,pics,counts=0;    while(scanf("%d",&N)!=EOF&&N){        for(i=1;i<=N;i++){            scanf("%d %d",&stone[i].x,&stone[i].y);        }        counts++;        getchar();        memset(vis,0,sizeof(vis));        for(i=1;i<=N;i++){            for(j=i;j<=N;j++){                maps[i][j]=sqrt(1.0*(stone[i].x-stone[j].x)*(stone[i].x-stone[j].x)+(stone[i].y-stone[j].y)*(stone[i].y-stone[j].y));                maps[j][i]=maps[i][j];                if(i==1)                    dis[j]=maps[i][j];            }        }        vis[1]=1;        for(i=1;i<=N;i++){            pics=findmin();            vis[pics]=1;            for(j=1;j<=N;j++){                if(!vis[j]&&dis[pics]>=maps[pics][j]&&dis[j]>dis[pics])                    dis[j]=dis[pics];                if(!vis[j]&&dis[pics]<maps[pics][j]&&dis[j]>maps[pics][j])                    dis[j]=maps[pics][j];            }            if(vis[2]==1)                break;        }        printf("Scenario #%d/nFrog Distance = %.3lf/n/n",counts,dis[2]);    }    return 0;}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表