【需改进】07-图5 Saving James Bond - Hard Version(30 分)

题目来源:中国大学MOOC-陈越、何钦铭-数据结构-2018春
作者: 陈越
单位: 浙江大学
问题描述:
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 by 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 a shortest path to reach one of the banks. The length of a path is the number of jumps that James has to make.
Input Specification:
Each input file contains one test case. Each case starts with a line containing two positive integers N (≤100), the number of crocodiles, and D, the maximum distance that James could jump. Then N lines follow, each containing the (x,y) location of a crocodile. Note that no two crocodiles are staying at the same position.
Output Specification:
For each test case, if James can escape, output in one line the minimum number of jumps he must make. Then starting from the next line, output the position (x,y) of each crocodile on the path, each pair in one line, from the island to the bank. If it is impossible for James to escape that way, simply give him 0 as the number of jumps. If there are many shortest paths, just output the one with the minimum first jump, which is guaranteed to be unique.
Sample Input 1:
17 15
10 -21
10 21
-40 10
30 -50
20 40
35 10
0 -10
-25 22
40 -40
-30 30
-10 22
0 11
25 21
25 10
10 10
10 35
-30 10
Sample Output 1:
4
0 11
10 21
10 35
Sample Input 2:
4 13
-12 12
12 12
-12 -12
12 -12
Sample Output 2:
0

解答:BFS求单源最短路径问题,我的解法比较复杂,留个档,有时间改进下。可以参考这里

#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
using namespace std;

int N,D;
const int INF=101;
const int MaxN=100;
int dis[MaxN];
int path[MaxN];
bool isVisited[MaxN];
queue<int> safePoint;

struct eyu
{
    int x,y;
};
eyu EachEyu[MaxN];

int returnDis(eyu a,eyu b)
{
    return (b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y);
}

bool isReach(eyu b)
{
    eyu a;
    a.x=0;
    a.y=0;
    int disAB=returnDis(a,b);
    if(disAB<=(7.5+D)*(7.5+D))
        return true;
    else
        return false;
}
bool isReach(eyu a, eyu b)
{
    if(returnDis(a,b)<=D*D)
        return true;
    else
        return false;
}
bool isSafe(eyu a)
{
    if(abs(a.x)+D>=50||abs(a.y)+D>=50)
        return true;
    else
        return false;
}
bool cmp(int a,int b)
{
    eyu zero;
    zero.x=0;
    zero.y=0;
    return returnDis(zero,EachEyu[a])<returnDis(zero,EachEyu[b]);
}
void bfs()
{
    //first turn
    queue<int> recordPoint;
    vector<int> order;
    for(int i=0;i<N;i++)
    {
        if(isReach(EachEyu[i]))
        {
            order.push_back(i);
            isVisited[i]=true;
            dis[i]=1;
            path[i]=-1;
            if(isSafe(EachEyu[i]))
            {
                safePoint.push(i);
            }
        }
    }
    sort(order.begin(),order.end(),cmp);
    for(int i=0;i<order.size();i++)
    {
        recordPoint.push(order[i]);
    }
    while(!recordPoint.empty())
    {
        int JameStand=recordPoint.front();
        recordPoint.pop();
        isVisited[JameStand]=true;
        for(int i=0;i<N;i++)
        {
            if(isReach(EachEyu[JameStand],EachEyu[i])&&isVisited[i]==false)
            {
                if(dis[JameStand]+1<dis[i])
                {
                    dis[i]=dis[JameStand]+1;
                    recordPoint.push(i);
                    path[i]=JameStand;
                    if(isSafe(EachEyu[i]))
                    {
                        safePoint.push(i);
                    }
                }
            }
        }
    }
}
void findResult()
{
    int MinLen=INF;
    int MinPoint;
    stack<int> safePath;
    /*for(int i=0;i<N;i++)
        cout<<dis[i]<<" ";
    cout<<endl;
    cout<<safePoint.empty()<<endl;*/
    while(!safePoint.empty())
    {
        int oneOfThePoint=safePoint.front();
        safePoint.pop();
        if(dis[oneOfThePoint]<MinLen)
        {
            MinLen=dis[oneOfThePoint];
            MinPoint=oneOfThePoint;
        }
    }
    if(MinLen==INF)
        cout<<"0";
    else
    {
        /*for(int i=0;i<N;i++)
            cout<<path[i]<<" ";
        cout<<endl;*/
        cout<<MinLen+1<<endl;
        while(path[MinPoint]!=-1)
        {
            safePath.push(MinPoint);
            MinPoint=path[MinPoint];
        }
        safePath.push(MinPoint);
        while(!safePath.empty())
        {
            int Point=safePath.top();
            safePath.pop();
            cout<<EachEyu[Point].x<<" "<<EachEyu[Point].y<<endl;
        }
    }
}
int main()
{
    cin>>N>>D;
    for(int i=0;i<N;i++)
    {
        cin>>EachEyu[i].x>>EachEyu[i].y;
    }
    if(D>=42.5)
    {
        cout<<"1";
        return 0;
    }
    fill(isVisited,isVisited+MaxN,0);
    fill(dis,dis+MaxN,INF);
    for(int i=0;i<MaxN;i++)
        path[i]=i;
    bfs();
    findResult();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值