题目:
Frogger
Description 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. 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 Source |
[Submit] [Go Back] [Status] [Discuss]
思路:
复制一下别人的题意,有两只青蛙和若干块石头,现在已知这些东西的坐标,两只青蛙A坐标和青蛙B坐标是第一个和第二个坐标,现在A青蛙想要到B青蛙那里去,并且A青蛙可以借助任意石头的跳跃,而从A到B有若干通路,问从A到B的所有通路上的最大边,比如有 有两条通路 1(4)5 (3)2 代表1到5之间的边为4, 5到2之间的边为3,那么该条通路跳跃范围(两块石头之间的最大距离)为 4, 另一条通路 1(6) 4(1) 2 ,该条通路的跳跃范围为6, 两条通路的跳跃范围分别是 4 ,6,我们要求的就是最小的那一个跳跃范围,即4,用三种方法都能解决
编译器选c++,别问为什么,我不知道
代码1:(Floyd)
#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <cmath>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define maxnum 300
#define inf 0x3f3f3f3f
using namespace std;
int x[maxnum],y[maxnum],n;
double map[maxnum][maxnum];
void floyd()
{
for(int k=1; k<=n; k++)
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
map[i][j]=min(map[i][j],max(map[i][k],map[k][j]));//许多通路中最长边中的最小边
}
int main()
{
int q=1;
while(~scanf("%d",&n)&&n)
{
mem(map,0);
for(int i=1; i<=n; i++)
scanf("%d%d",&x[i],&y[i]);
for(int i=1; i<=n; i++)
for(int j=i+1; j<=n; j++)
map[i][j]=map[j][i]=sqrt(double(x[i]-x[j])*(x[i]-x[j])+double(y[i]-y[j])*(y[i]-y[j]));
floyd();
printf("Scenario #%d\nFrog Distance = %.3lf\n\n",q++,map[1][2]);
}
return 0;
}
代码2(dijkstra):
#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <cmath>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define maxnum 300
#define inf 0x3f3f3f3f
using namespace std;
int x[maxnum],y[maxnum],n;
double map[maxnum][maxnum];
double dis[maxnum];
int vis[maxnum];
void dj(int s)
{
mem(vis,0);
for(int i=1; i<=n; i++)
dis[i]=inf;//这里最好别用memset
dis[s]=0;
for(int i=1; i<=n; i++)
{
int minn=inf,k;
for(int j=1; j<=n; j++)
if(vis[j]==0&&dis[j]<minn)
{
k=j;
minn=dis[j];
}
vis[k]=1;
for(int j=1; j<=n; j++)
dis[j]=min(dis[j],max(dis[k],map[k][j]));//dis[j]为从一号石头到第j号石头所有通路中最长边中的最小边
}
}
int main()
{
int q=1;
while(~scanf("%d",&n)&&n)
{
mem(map,0);
for(int i=1; i<=n; i++)
scanf("%d%d",&x[i],&y[i]);
for(int i=1; i<=n; i++)
for(int j=i+1; j<=n; j++)
map[i][j]=map[j][i]=sqrt(double(x[i]-x[j])*(x[i]-x[j])+double(y[i]-y[j])*(y[i]-y[j]));
dj(1);
printf("Scenario #%d\nFrog Distance = %.3lf\n\n",q++,dis[2]);
}
return 0;
}
代码3(SPFA):
#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <cmath>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define maxnum 300
#define inf 0x3f3f3f3f
using namespace std;
int x[maxnum],y[maxnum],n;
double map[maxnum][maxnum];
double dis[maxnum];
int vis[maxnum];
void spfa()
{
queue<int>q;
for(int i=1; i<=n; i++)
dis[i]=inf;
dis[1]=0;
for(int i=1; i<=n; i++)
vis[i]=0;
vis[1]=1;
q.push(1);
while(!q.empty())
{
int k=q.front();
vis[k]=0;
q.pop();
for(int j=1; j<=n; j++)
if(max(dis[k],map[k][j])<dis[j])
{
dis[j]=max(dis[k],map[k][j]);
if(vis[j]==0)
{
q.push(j);
vis[j]=1;
}
}
}
}
int main()
{
int q=1;
while(~scanf("%d",&n)&&n)
{
mem(map,0);
for(int i=1; i<=n; i++)
scanf("%d%d",&x[i],&y[i]);
for(int i=1; i<=n; i++)
for(int j=i+1; j<=n; j++)
map[i][j]=map[j][i]=sqrt(double(x[i]-x[j])*(x[i]-x[j])+double(y[i]-y[j])*(y[i]-y[j]));
spfa();
printf("Scenario #%d\nFrog Distance = %.3lf\n\n",q++,dis[2]);
}
return 0;
}