OpenJudge 2253:Frogger

来源:互联网 发布:盐与避难所 mac 汉化 编辑:程序博客网 时间:2024/05/29 11:43

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.
输入
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.
输出
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.
样例输入
2
0 0
3 4

3
17 4
19 4
18 5

0
样例输出
Scenario #1
Frog Distance = 5.000

Scenario #2
Frog Distance = 1.414

题目的大概意思就是说,有两只青蛙,一只青蛙A要到另一只青蛙B的石头上去。他们之间也有很多的石头,定义frog distance为在一路径上,青蛙跳过最远的距离。求所有路径中最短的frog distance

本题可以借鉴Dijkstra算法,Dijkstra算法是:(distance[i]表示起点0到i的已知最短距离——称为已知是因为还会不断更新;distance[0]=0)
1.找出distance数组中最小的距离distance[s]且 之前没有jumped过
2.加入新节点——jumped distance[s],并把s当作中间节点,进行下一步扩展;
3.更新——如果distance[k]>distance[s]+dist(s,k) (k是没有jumped的节点),那么就用distance[s]+dist(s,k)替换distance[k]的值;
4.重复1,2,3,直到所有节点都jumped过;

最后的结果中,我们发现distance数组中 distance[i]就表示起点到i的最短距离——因为jumped一个节点就表示 该节点的distance是最短距离了。

所以我们可以把其中 distance[i]的意义改成为 表示起点到i的所有路径中的已知最短的frog distance;
然后再稍微改一下限制条件就可以了——在3.更新的时候,如果distance[k]>Max(distance[s],dist(s,k)) (k是没有jumped的节点),那么就用Max(distance[s],dist(s,k))替换distance[k]的值;

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <math.h>#include <stdbool.h>#define maxCo 200#define inf 20000#define max(a,b) (a>b?a:b)int coX[maxCo];int coY[maxCo];int coNum;bool jumped[maxCo];float distance[maxCo];float result;int caseN=1;float dist(int x,int y){  return sqrt( pow((coX[x]-coX[y]),2)+pow((coY[x]-coY[y]),2));}void dijkstra(){  int i,minIndex;  int minDist=inf;  for(i=0;i<coNum;i++){    // initially the jumped[0] is false but distance[0] is 0    if(!jumped[i]&& minDist>distance[i]){      minDist=distance[i];      minIndex=i;    }  }  //minIndex is the middle stone now,and jumped it  jumped[minIndex]=true;  if(minIndex==1){    result=distance[minIndex];    return;  }  //using the minIndex to test the other stone  for(i=0;i<coNum;i++){    if(!jumped[i] && distance[i]>max( dist(i,minIndex),distance[minIndex] ))      distance[i]=max( dist(i,minIndex),distance[minIndex] );  }  dijkstra();}int main(int argc,const char * argv[]){    //printf("%lu",sizeof(coX));  while (scanf("%d",&coNum)) {    if(coNum==0)      break;    memset(coX,0,sizeof(coX));    memset(coY,0,sizeof(coY));    int cc=0;    for(cc=0;cc<maxCo;cc++)      distance[cc]=inf;    distance[0]=0;    memset(jumped,false,sizeof(jumped));    int i;    for(i = 0;i<coNum;i++){      scanf("%d%d",&coX[i],&coY[i]);    }    result=dist(0,1);    dijkstra();      printf("Scenario #%d\n", caseN);      printf("Frog Distance = %.3f\n\n",result);      caseN++;  }  printf("\n");  return 0;}
0 0
原创粉丝点击