杭电1245

Saving James Bond

Time Limit: 6000/3000 MS (Java/Others)
Memory Limit: 65536/32768 K (Java/Others)

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

思路:

Floyd算法,构图有点麻烦。

  • path[i][j]:若两点间距离小于等于d,则两点间距离转换为边的权值 ;
  • path[0][i]:以原点为中心,7.5为半径画圆,若某一点到原点距离大于7.5,小于等于d+7.5,则权值设为与原点距离-7.5(即到该圆的垂直距离);
  • path[i][n+1]:点与边界的横轴距离或纵轴距离小于d,则权值设为点与边界的横、纵轴距离的最小值。

AC代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <math.h>
using namespace std;

#define INF 0xfffffff
#define maxn 105

double path[maxn][maxn];
int step[maxn][maxn];
double x[maxn],y[maxn];
int n;
double d;

double mmin(double a,double b)
{
    return a>b?b:a;
}

void floyd()
{
    for(int k=0;k<=n+1;k++)
    {
        for(int i=0;i<=n+1;i++)
        {
            if(path[i][k]!=INF)
            {
                for(int j=0;j<=n+1;j++)
                {
                    if(path[i][j]>path[i][k]+path[k][j])
                    {
                        path[i][j]=path[i][k]+path[k][j];
                        step[i][j]=step[i][k]+step[k][j];
                    }
                    else if(path[i][k]+path[k][j]==path[i][j])
                    {
                        if(step[i][j]>step[i][k]+step[k][j])
                        {
                            step[i][j]=step[i][k]+step[k][j];
                        }
                    }
                }
            }
        }
    }
}

int main()
{
    while(cin>>n>>d)
    {
        //init
        for(int i=0;i<=n+1;i++)
        {
            for(int j=0;j<=n+1;j++)
            {
                if(i==j)
                {
                    path[i][j]=0;
                    step[i][j]=0;
                }
                else 
                {
                    path[i][j]=INF;
                    step[i][j]=1;
                }
            }
        }
        //input
        for(int i=1;i<=n;i++)
        {
            cin>>x[i]>>y[i];
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                //两点间距离小于等于d,则将距离转换为边的权值 
                if(sqrt(fabs(x[i]-x[j])*fabs(x[i]-x[j])+fabs(y[i]-y[j])*fabs(y[i]-y[j]))<=d)
                {
                    path[i][j]=path[j][i]=sqrt(fabs(x[i]-x[j])*fabs(x[i]-x[j])+fabs(y[i]-y[j])*fabs(y[i]-y[j]));
                }
            }
            //若到原点距离大于7.5,小于等于d+7.5
            if(sqrt(fabs(x[i])*fabs(x[i])+fabs(y[i])*fabs(y[i]))-7.5<=d)
            {
                //权值设为与原点距离-7.5 
                if(sqrt(fabs(x[i]-0)*fabs(x[i]-0)+fabs(y[i]-0)*fabs(y[i]-0))-7.5>0)
                {
                    path[0][i]=sqrt(fabs(x[i]-0)*fabs(x[i]-0)+fabs(y[i]-0)*fabs(y[i]-0))-7.5;
                }
            }
            //点与边界的横轴距离或纵轴距离小于d
            //权值设为点与边界的横、纵轴距离的最小值 
            if(fabs(fabs(x[i])-50)<=d||fabs(fabs(y[i])-50)<=d)
            {
                path[i][n+1]=mmin(fabs(fabs(x[i])-50),fabs(fabs(y[i])-50));
            }
        }
        floyd();
        if(path[0][n+1]==INF)
        {
            cout<<"can't be saved"<<endl;
        }
        else printf("%.2lf %d\n",path[0][n+1],step[0][n+1]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/albert_bolt/article/details/81172976
今日推荐