POJ 2253 Frogger

来源:互联网 发布:树莓派读取温湿度数据 编辑:程序博客网 时间:2024/06/10 07:39



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. 
Input
The 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.
Output
For 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 Input
20 03 4317 419 418 50
Sample Output
Scenario #1Frog Distance = 5.000Scenario #2Frog Distance = 1.414

StatusAcceptedTime16msMemory564kBLength1480LangC++Submitted2017-04-28 20:54:22题目大意:

N个石头 
每个石头的坐标
第一个---起点
第二个---终点
注意:每次输出都是两个回车 

要输出的是,在这些不同能到达目的地的路径中,找的每条路径的跳跃距离,在这些跳跃距离中找到哪个最小的跳跃距离即可。

核心代码:

for(j=1;j<=n;j++)
dist[j]=min(dist[j],max(dist[k],map[k][j]));

1-j的
最小跳跃距离存在dist[j]数组里
max(dist[k],map[k][j])---青蛙最小跳跃距离>= 两块石头之间的最大距离
dist[k]---已经是最小1-k的最小跳跃距离,但是如今你需要从k到j去,所以要找到大的距离作为跳跃距离。
在所有能通往目的地的不同路径中,找到每一个路径中的最大距离,在这些最大距离中找到一个最小的。 
原本存在的dist[j],与经过k的最小跳跃距离 进行比较,找到短的赋值给dist[j]
------1-j石头中找到最小跳跃距离
dist[j]=min(dist[j],max(dist[k],map[k][j]))
------在最小跳跃距离中的最小距离


也就是说,在所有通往目的地的路径中,找到每条路径的最大值,比较这些路径的最大值,找出最小的

一个就是了。

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespace std;#define MAXN 207#define INF 1<<30struct number{int x,y;}stone[MAXN];int n;double dist[MAXN];double map[MAXN][MAXN];double distab(int i,int j){int x,y;x=stone[i].x-stone[j].x;y=stone[i].y-stone[j].y;return sqrt((double)(x*x+y*y));//距离小数 }void dijkstar(){bool flag[MAXN];int i,j,h;for(i=1;i<=n;i++)dist[i]=INF;memset(flag,false,sizeof(flag));for(i=1;i<=n;i++)for(j=i;j<=n;j++)map[i][j]=map[j][i]=distab(i,j);dist[1]=0;for(i=1;i<=n;i++){int temp=INF;int k;for(j=1;j<=n;j++){if((!flag[j])&&temp>dist[j]){temp=dist[j];k=j;}}flag[k]=true;for(j=1;j<=n;j++)dist[j]=min(dist[j],max(dist[k],map[k][j]));} }int main(){int num,i;num=0;while(cin>>n&&n){for(i=1;i<=n;i++)cin>>stone[i].x>>stone[i].y;/*for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)if(i!=j)map[i][j]=INF;*/dijkstar();printf("Scenario #%d\n",++num);printf("Frog Distance = %.3lf\n\n",dist[2]); }return 0; }   /* 注意输出回车 空格的限制 */ 


0 0
原创粉丝点击