Saving James Bond
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1258 Accepted Submission(s): 230
Problem Description
This time let us consider the situation in the movie "Live and Let Die" in which James Bond, the world's most famous spy, was captured by a group of drug dealers. He was sent to a small piece of land at the center of a lake filled with crocodiles. There he performed the most daring action to escape -- he jumped onto the head of the nearest crocodile! Before the animal realized what was happening, James jumped again onto the next big head... Finally he reached the bank before the last crocodile could bite him (actually the stunt man was caught by the big mouth and barely escaped with his extra thick boot).
Assume that the lake is a 100×100 square one. Assume that the center of the lake is at (0,0) and the northeast corner at (50,50). The central island is a disk centered at (0,0) with the diameter of 15. A number of crocodiles are in the lake at various positions. Given the coordinates of each crocodile and the distance that James could jump, you must tell him whether he could escape.If he could,tell him the shortest length he has to jump and the min-steps he has to jump for shortest length.
Assume that the lake is a 100×100 square one. Assume that the center of the lake is at (0,0) and the northeast corner at (50,50). The central island is a disk centered at (0,0) with the diameter of 15. A number of crocodiles are in the lake at various positions. Given the coordinates of each crocodile and the distance that James could jump, you must tell him whether he could escape.If he could,tell him the shortest length he has to jump and the min-steps he has to jump for shortest length.
Input
The input consists of several test cases. Each case starts with a line containing n <= 100, the number of crocodiles, and d > 0, the distance that James could jump. Then one line follows for each crocodile, containing the (x, y) location of the crocodile. Note that x and y are both integers, and no two crocodiles are staying at the same position.
Output
For each test case, if James can escape, output in one line the shortest length he has to jump and the min-steps he has to jump for shortest length. If it is impossible for James to escape that way, simply ouput "can't be saved".
Sample Input
4 10 17 0 27 0 37 0 45 0 1 10 20 30
Sample Output
42.50 5 can't be saved迪杰斯特拉算法;/*wrong:求路过大神调试*/ #include<iostream> #include<cmath> #include<iomanip> using namespace std; #define MAX 102 #define inf 1000000000 typedef struct infor { double x , y ; }infor ; int n ,v[MAX]; double s[MAX][MAX] ; void shortpath() { int i,j ,k ,step=1 ; double d[MAX]; for( i = 0 ; i <= n ; i++) d[i] = s[0][i] ; d[0] = 0 ; v[0] = 1; for( i = 0 ; i<=n; i++) { double min1 = inf + 1; for(j= 0 ; j<= n ;j++) if(!v[j] && min1 > d[j]) { k= j; min1 = d[j] ; } v[k] = 1; for( j = 0 ; j <= n ; j++) if(!v[j] && (min1 + s[k][j]) < d[j]){ d[j] = min1 + s[k][j] ; step ++ ; } } if(d[n] == inf ) cout<<"can't be saved"<<endl; else cout<<fixed<<setprecision(2)<<d[n]<<" "<<step<<endl; } double min(double a, double b) { return a>b?b:a; } int main(void) { double len ; while(cin>>n>>len) { memset(v,0,sizeof(v)); int i , j ; infor a[MAX]; for( i = 1 ; i <=n ; i++) cin>>a[i].x >> a[i].y ; a[0].x = a[0].y = 0 ; n=n+1 ; a[n].x = a[n].y = 50 ; for( i= 0 ; i <= n ; i++) for( j = 0 ; j <= n ; j++) { s[i][j] = s[j][i] = sqrt((a[i].x- a[j].x)*(a[i].x-a[j].x) + (a[i].y-a[j].y)*(a[i].y-a[j].y)); if(i== n) s[i][j] = s[j][i] = min(50 - fabs(a[j].x),50-fabs(a[j].y) ); if( j == n ) s[i][j] = s[j][i] = min(50 - fabs(a[i].x),50-fabs(a[i].y )); if((i == 0 || j == 0 ) && s[i][j] >= 7.5) s[i][j] = s[j][i] = s[i][j] - 7.5 ; if(i == j) s[i][j] = s[j][i] = 0 ; if(s[i][j] > len) s[i][j] = s[j][i] = inf ; } shortpath(); } return 0; }