OpenJudge 2253:Frogger

本文介绍了一道关于寻找两块石头间最小跳跃距离的算法题目,通过修改Dijkstra算法来解决这个问题,并给出了具体的实现代码。

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

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值