hdu1245 Saving James Bond(flody)

Saving James Bond

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 712    Accepted Submission(s): 136


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.
 

 

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
 
题意:有一个100*100正方形,James Bond 在正方形的中心圆盘中,圆盘的直径是以15,给你n个点随机分布在这个正方形中,
James Bond 可以跳跃在这些点上,但必须两点间的距离小于d。输出最小的跳跃距离和步数。
 
分析:首先计算每个坐标两两间的距离,然后找出所以可以从圆盘直接一步到达的点存入s[]数组中,找出最后离开正方形的点存入e[]中。
设置两个虚点u,v;用u 与 s[] 所以顶点相连,用 v 与 e[] 所以顶点相连。
最终可以转换成求 u 到 v 的最短路,由于顶点只有100个所以 flody 和 dijkstra 都行。
我采用的是 floyd 主要是写起来很方便,不容易出错。
View Code
  1 #include<iostream>
  2 #include<cstring>
  3 #include<cmath>
  4 #include<cstdio>
  5 #define N 110
  6 #define INF 100000000.0
  7 using namespace std;
  8 
  9 double dp[N][N],dp2[N][N];
 10 int s[N],e[N],step[N][N];
 11 struct SS{int x,y;}point[N];
 12 
 13 void floyd(int n)
 14 {
 15     int i,j,k;
 16     for(k=0;k<=n;k++)
 17     {
 18         for(i=0;i<=n;i++)
 19         {
 20             for(j=0;j<=n;j++)
 21             {
 22                 if(dp[i][j]>dp[i][k]+dp[k][j])
 23                 {
 24                     dp[i][j]=dp[i][k]+dp[k][j];
 25                     step[i][j]=step[i][k]+step[k][j];
 26                 }
 27             }
 28         }
 29     }
 30 }
 31 
 32 double min(double a,double b)
 33 {
 34     return a<b?a:b;
 35 }
 36 
 37 int main()
 38 {
 39     int n,i,j;
 40     double d,xx,yy;
 41     while(cin>>n>>d)
 42     {
 43         j=1;
 44         for(i=1;i<=n;i++)
 45         {
 46             cin>>xx>>yy;
 47             if(fabs(xx)<=7.5&&fabs(yy)<=7.5) continue;
 48             point[j].x=xx;
 49             point[j].y=yy;
 50             j++;
 51         }
 52         n=j;
 53         if(n==1)
 54         {
 55             if(d>=42.5) printf("42.50 1\n");
 56             else printf("can't be saved\n");
 57             continue;
 58         }
 59         for(i=0;i<=n;i++)
 60             for(j=0;j<=n;j++)
 61             {
 62                 dp[i][j]=INF;
 63                 step[i][j]=0;
 64             }
 65         for(i=1;i<n;i++)
 66         {
 67             for(j=1;j<n;j++)
 68             {
 69                 if(i==j)
 70                 {
 71                     dp[i][j]=0;continue;
 72                 }
 73                 dp[i][j]=sqrt(pow(point[i].x-point[j].x,2)+pow(point[i].y-point[j].y,2));
 74                 step[i][j]=1;
 75                 if(dp[i][j]>d)
 76                 {
 77                     dp[i][j]=INF;
 78                     step[i][j]=0;
 79                 }
 80             }
 81         }
 82         int len1,len2;
 83         len1=len2=0;
 84         for(i=1;i<n;i++)
 85         {
 86             if(sqrt(pow(point[i].x,2)+pow(point[i].y,2))<=7.5+d)
 87             {
 88                 s[len1]=i;len1++;
 89             }
 90             if((50-point[i].x<=d)||(point[i].x+50<=d)||(point[i].y+50<=d)||(50-point[i].y<=d))
 91             {
 92                 e[len2]=i;len2++;
 93             }
 94         }
 95         for(i=0;i<len1;i++)
 96         {
 97             dp[s[i]][0]=dp[0][s[i]]=sqrt(pow(point[s[i]].x,2)+pow(point[s[i]].y,2))-7.5;
 98             step[s[i]][0]=step[0][s[i]]=1;
 99         }
100         for(i=0;i<len2;i++)
101         {
102             dp[e[i]][n]=min(50-point[e[i]].x,50+point[e[i]].x);
103             dp[e[i]][n]=min(dp[e[i]][n],50+point[e[i]].y);
104             dp[e[i]][n]=min(dp[e[i]][n],50-point[e[i]].y);
105             dp[n][e[i]]=dp[e[i]][n];
106             step[n][e[i]]=step[e[i]][n]=1;
107         }
108         floyd(n);
109         if(dp[0][n]==INF) printf("can't be saved\n");
110         else printf("%.2lf %d\n",dp[0][n],step[0][n]);
111     }
112     return 0;
113 }

 

转载于:https://www.cnblogs.com/zhourongqing/archive/2012/05/30/2527146.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值