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. 
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
2
0 0
3 4

3
17 4
19 4
18 5

0
Sample Output
Scenario #1
Frog Distance = 5.000

Scenario #2
Frog Distance = 1.414

题意:小青蛙Freddy想去看小青蛙Fiona,他们中间有很多石头,Freddy通过在石头之间跳跃到达Fiona那里,因为Freddy比较懒,所以他希望每次跳的距离尽可能的短,而且,在每一条路中,他要跳的距离是所有距离最大的那个。比如第二个样例,从第一个点到第二个点有两条路线,1→2和1→3→2。1→2之间要跳2,1→3、3→2之间的距离都为sqrt(2),所以我们选择第二个方案,因为第二个方案每次Freddy只要跳sqrt(2)。

思路:最短路的变形~

代码1号:Floyd-Warshall

#include<stdio.h>//Floyd-Warshall
#include<math.h>
#include<algorithm>
using namespace std;
int main()
{
    int n,g=1;
    double x[250],y[250],a[250][250];
    while(~scanf("%d",&n)&&n)
    {
        for(int i=0;i<n;i++)
            scanf("%lf %lf",&x[i],&y[i]);
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                a[i][j]=a[j][i]=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
        for(int k=0;k<n;k++)
            for(int i=0;i<n;i++)
                for(int j=0;j<n;j++)
                    a[i][j]=min(a[i][j],max(a[i][k],a[j][k]));
        printf("Scenario #%d\n",g++);
        printf("Frog Distance = %.3f\n\n",a[0][1]);
    }
    return 0;
}

代码2号:Dijkstra

#include<stdio.h>//Dijkstra
#include<math.h>
#include<algorithm>
#define inf 0x3f3f3f
using namespace std;
int main()
{
    int n,g=1;
    double x[250],y[205],dis[250],a[250][250];
    int vis[250];
    while(~scanf("%d",&n),n)
    {
       for(int i=0;i<n;i++)
            scanf("%lf %lf",&x[i],&y[i]);
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                a[i][j]=a[j][i]=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
        for(int i=0;i<n;i++)
        {
            dis[i]=a[0][i];
            vis[i]=0;
        }
        vis[0]=1;
        for(int i=0;i<n-1;i++)
        {
            int k;
            double mi=inf;
            for(int j=0;j<n;j++)
            {
                if(vis[j]==0&&dis[j]<mi)
                {
                    mi=dis[j];
                    k=j;
                }
            }
            vis[k]=1;
            for(int j=0;j<n;j++)
                dis[j]=min(dis[j],max(dis[k],a[k][j]));
        }
        printf("Scenario #%d\n",g++);
        printf("Frog Distance = %.3f\n\n",dis[1]);
    }
    return 0;
}

代码3号:spfa

没有完全掌握~

然后spfa()那一段是别人的代码~

点击打开原代码链接

#include<stdio.h>
#include<math.h>
#include<queue>
#include<algorithm>
#define inf 0x3f3f3f
using namespace std;
int n,g=1,vis[250];
double x[250],y[250],dis[250],a[250][250];
void spfa()
{
    queue<int>q;
    int i,j;
    for(i=1; i<n; i++)
    {
        dis[i]=inf;
        vis[i]=0;
    }
    dis[0]=0;
    q.push(0);
    vis[0]=1;
    while(!q.empty())
    {
        i=q.front();
        q.pop();
        vis[i]=0;
        for(j=0; j<n; j++)
        {
            if(dis[j]>max(dis[i],a[i][j]))
            {
                dis[j]=max(dis[i],a[i][j]);
                if(vis[j]==0)
                {
                    q.push(j);
                    vis[j]=1;
                }
            }
        }
    }
}
int main()
{
    while(~scanf("%d",&n)&&n)
    {
        for(int i=0; i<n; i++)
            scanf("%lf %lf",&x[i],&y[i]);
        for(int i=0; i<n; i++)
            for(int j=0; j<n; j++)
                a[i][j]=a[j][i]=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
        spfa();
        printf("Scenario #%d\n",g++);
        printf("Frog Distance = %.3f\n\n",dis[1]);
    }
    return 0;
}
阅读更多

Frogger's For Dinner

10-09

Descriptionnn"Uncle Jacques, " you ask, "What's for dinner?" nn"Ask me again in 10 minutes, " Uncle Jacques replies, eyeing the weary-looking frog sitting on the shoulder of Interstate 10, in front of your dilapidated shack. nnYou notice the potential roadkill as it begins its journey across the vehicle-laden road. You want to know if you should begin boiling a pot of water in anticipation of frog legs for dinner or warm up the leftover possum. You fire up your Swamp 'Puter XL2 and quickly write a program to determine if it is possible for the frog to make it across the road or if it will be hit by a vehicle. nnExamining the patch of road in front of your shack, you notice the lanes and shoulders resemble a 10 X 10 grid of squares (shown below). You also notice that the way the frog and the vehicles are moving can be described in "turns". To determine if the frog makes it across the road, you quickly devise a set of rules: nAt the onset of a run, the frog can start in any square on row 0 (the starting shoulder). nAt the onset of a run, each vehicle will occupy a square in any column, but only in rows 1-8 (the lanes). nEach turn will consist of two steps: nFirst, the frog will always remain in the same column and move one row down, towards row 9, his destination (he's not the smartest frog in the world). nNext, all the vehicles move (at the same time), n squares left or right, depending on which row (lane) they are in, where n is their speed (given in the input). To simulate more approaching vehicles, if a vehicle moves off the grid, it instead "wraps around" and appears from the opposite side. Ex: In the grid below, if a vehicle would move to occupy column -1, it would instead occupy column 9 (column -2 would instead occupy column 8, etc.). Also, if a vehicle would move to occupy column 10, it would instead occupy column 0 (column 11 would instead occupy column 1, etc.). n Columnnn 0123456789nn ----------nnR 0| |<- The frog can start in any square on row 0 nno 1| |(shoulder)nnw 2| /___ | nn 3| \ |cars in rows (lanes) 1-4 move left, ornn 4| |towards column 0nn 5| |nn 6| ___\ |cars in rows (lanes) 5-8 move right, ornn 7| / |towards column 9nn 8| |nn 9| |<- The destination row (shoulder) of the frognn ----------nnThe frog will succeed in crossing the interstate for a run if it can reach row 9 (without becoming roadkill) after a series of turns starting in ANY column on row 0 (he's not the dumbest frog in the world, either). nThe frog will become roadkill if at any point it occupies the same square as a vehicle. This includes: nThe frog moving into a square a vehicle occupies, or nA vehicle "running over" the frog by moving over or into a square the frog occupies.nnInputnnInput to this problem will consist of a (non-empty) series of up to 100 data sets. Each data set will describe the starting conditions of the interstate for a run and will be formatted according to the following description. There will be no blank lines separating data sets. nStart line - A single line, "START" nThe next 8 lines will represent rows 1-8 (the "lanes" of the interstate), starting with row 1. Each line will consist of 10 integers, separated by single spaces. Each integer will represent a column for that row and will be either: n0, representing no vehicle occupying that square, or na non-zero integer N in the range 1 <= N <= 9, representing a vehicle is occupying that square and the non-zero integer is its speed. NOTE: The given speeds will NOT result in vehicles moving over other vehicles or into a square occupied by another vehicle (no accidents), since all the vehicles move at the same time and all vehicles on a given row are guaranteed to move at the same speed.nnEnd line - A single line, "END"nnOutputnnOutput for each data set will be exactly one line of output. The line will either be "LEFTOVER POSSUM" or "FROGGER" (both all caps with no whitespace leading or following). nn"LEFTOVER POSSUM" will appear if the frog can make it safely (without becoming roadkill) across the interstate after a series of turns starting in ANY column on row 0. nn"FROGGER" will be output for a data set if it fails to meet the criteria for a "LEFTOVER POSSUM" line.nSample InputnnSTARTn3 0 0 0 0 3 0 0 0 3n1 0 0 0 1 0 0 0 0 0n4 0 0 0 0 0 0 4 0 0n0 0 2 0 0 0 0 0 0 2n5 0 0 0 0 0 0 0 0 0n0 2 0 0 0 0 0 2 0 2n0 0 0 4 0 0 0 0 0 0n0 2 0 0 0 0 0 0 0 0nENDnSTARTn9 9 9 9 9 9 9 9 9 9n0 0 0 0 0 0 0 0 0 0n0 0 0 0 0 0 0 0 0 0n0 0 0 0 0 0 0 0 0 0n0 0 0 0 0 0 0 0 0 0n0 0 0 0 0 0 0 0 0 0n0 0 0 0 0 0 0 0 0 0n0 0 0 0 0 0 0 0 0 0nENDnSTARTn1 0 0 0 0 0 0 0 0 0n0 1 0 0 0 0 0 0 0 0n0 0 1 0 0 0 0 0 0 0n0 0 0 1 0 0 0 0 0 0n0 0 0 1 0 0 0 0 0 0n0 0 0 0 1 0 0 0 0 0n0 0 0 0 0 0 0 0 0 0n0 0 0 0 0 0 0 0 0 0nENDnSample OutputnnFROGGERnFROGGERnLEFTOVER POSSUM

没有更多推荐了,返回首页