Saving James Bond
Saving James Bond
Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
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
//有个缺憾,未能AC
//但纠结了那么久,实在找不出错
//难得也纪念一下
//若哪位有兴趣,帮忙看看,那就感激不尽了
# include<iostream> # include<cstdio> # include<cstring> # include<cmath> # define inf 0x3f3f3f3f using namespace std; int nNum; double step; double Map[150][105]; double dis[105]; double Min; int vis[105],mink; int cnt[105]; const double eps=1e-8;//听说卡精度,就加了一个 struct PointNode { double x; double y; }point[105]; double distance(double x1,double y1,double x2,double y2) { return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); } void dij()//用Dijkstra算法 { int i,j,leap=0,mStep; double d; for(i=0;i<=nNum;i++) { dis[i]=inf*1.0; } dis[0]=0; for(i=1;i<=nNum;i++) { for(j=0,Min=inf*1.0;j<=nNum;j++) { if(!vis[j]&&dis[j]<Min-eps) { Min =dis[j]; mink=j; } } if(fabs(Min-inf*1.0)<=eps) { break; } vis[mink]=1; for(j=1;j<=nNum;j++) { if(!vis[j]&&dis[j]>dis[mink]+Map[mink][j]+eps) { dis[j]=dis[mink]+Map[mink][j]; cnt[j]=cnt[mink]+1; } else if(!vis[j]&&fabs(dis[j]-(dis[mink]+Map[mink][j]))<=eps&&cnt[j]>cnt[mink]+1)//在同等距离的情况下,选择步数更少的那条 cnt[j]=cnt[mink]+1; } } Min=inf*1.0; for(i=1;i<=nNum;i++) { if((fabs(point[i].x)>=50-step-eps||fabs(point[i].y)>=50-step-eps)&&inf*1.0>eps+dis[i])//判断是否有能到达岸边的点,有点小技巧 { d=(50-fabs(point[i].x))<=(50-fabs(point[i].y)+eps)?(50-fabs(point[i].x)):(50-fabs(point[i].y)); if(Min>d+dis[i]+eps) { leap=1; Min=d+dis[i]; mStep=cnt[i]+1; } } } if(leap==1)printf("%0.2lf %d\n",Min,mStep); else printf("can't be saved\n"); } int main() { int i,j; while(cin>>nNum>>step) { memset(vis,0,sizeof(vis)); memset(cnt,0,sizeof(cnt)); for(i=0;i<=nNum;i++) { for(j=0;j<=nNum;j++) { if(i==j) Map[i][j]=0; else Map[i][j]=inf*1.0; } } for(i=1;i<=nNum;i++) { cin>>point[i].x>>point[i].y; if(distance(point[i].x,point[i].y,0,0)<=7.5+step+eps) { Map[0][i]=distance(point[i].x,point[i].y,0,0)-7.5; } } if(step>=42.5-eps) { printf("42.50 1\n"); continue; } for(i=1;i<=nNum-1;i++) { for(j=i+1;j<=nNum;j++) { if(distance(point[i].x,point[i].y,point[j].x,point[j].y)<=step+eps)//将坐标处理成边间的关系 { Map[i][j]=distance(point[i].x,point[i].y,point[j].x,point[j].y); Map[j][i]=Map[i][j]; } } } dij(); } return 0; }