POJ 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.InputThe 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.OutputFor 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 Input20 03 4317 419 418 50Sample OutputScenario #1Frog Distance = 5.000Scenario #2Frog Distance = 1.414
求的是路徑中兩相鄰點最大距離的最小值。
wa到懷疑人生,一度以為是自己算法錯誤,于是去驗證自己的方法,越驗證越覺得沒有問題。最后發(fā)現(xiàn)是看錯了題目!!!
第二只青蛙的位置是第2組輸入的數(shù)據(jù),一直以為是第N組……比賽的時候如果犯這種錯誤,那是要悔斷腸子的啊TAT
一定要仔細審題!!
回到正題,個人覺得這道題目還是挺有意思的。通過借鑒Dijkstra的思路可以順利做出,所以要好好理解Dijkstra,這里的dis數(shù)組存儲的是從起點到各個點的路徑中兩相鄰點最大距離的最小值,和Dijkstra一樣,每次找到dis值最小的未訪問點,然后以這個點為中心來更新dis的數(shù)值,新的dis[i]值有三種可能:
1)中心的dis值
2)中心到i點的距離
3)維持初始值。
#include<cstdio>#include<cstring>#include<algorithm>#include<math.h>#define maxn 210using namespace std;int N;typedef struct{ int x,y;}node;node stone[maxn];bool vis[maxn];double dis[maxn];double maps[maxn][maxn];int findmin(){ int i,flag,mins=10086; for(i=1;i<=N;i++){ if(!vis[i]&&dis[i]<=mins){ flag=i; mins=dis[i]; } } return flag;}int ends(){ int i; for(i=1;i<=N;i++){ if(vis[i]==0){ return 0; } } return 1;}int main(){ int i,j,k,pics,counts=0; while(scanf("%d",&N)!=EOF&&N){ for(i=1;i<=N;i++){ scanf("%d %d",&stone[i].x,&stone[i].y); } counts++; getchar(); memset(vis,0,sizeof(vis)); for(i=1;i<=N;i++){ for(j=i;j<=N;j++){ maps[i][j]=sqrt(1.0*(stone[i].x-stone[j].x)*(stone[i].x-stone[j].x)+(stone[i].y-stone[j].y)*(stone[i].y-stone[j].y)); maps[j][i]=maps[i][j]; if(i==1) dis[j]=maps[i][j]; } } vis[1]=1; for(i=1;i<=N;i++){ pics=findmin(); vis[pics]=1; for(j=1;j<=N;j++){ if(!vis[j]&&dis[pics]>=maps[pics][j]&&dis[j]>dis[pics]) dis[j]=dis[pics]; if(!vis[j]&&dis[pics]<maps[pics][j]&&dis[j]>maps[pics][j]) dis[j]=maps[pics][j]; } if(vis[2]==1) break; } printf("Scenario #%d/nFrog Distance = %.3lf/n/n",counts,dis[2]); } return 0;}
新聞熱點
疑難解答