【需改进】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 
#include 
#include 
#include 
#include 
#include 
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])void bfs()
{
    //first turn
    queue<int> recordPoint;
    vector<int> order;
    for(int i=0;iif(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;iwhile(!recordPoint.empty())
    {
        int JameStand=recordPoint.front();
        recordPoint.pop();
        isVisited[JameStand]=true;
        for(int i=0;iif(isReach(EachEyu[JameStand],EachEyu[i])&&isVisited[i]==false)
            {
                if(dis[JameStand]+11;
                    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
    while(!safePoint.empty())
    {
        int oneOfThePoint=safePoint.front();
        safePoint.pop();
        if(dis[oneOfThePoint]if(MinLen==INF)
        cout<<"0";
    else
    {
        /*for(int i=0;i
        cout<1<while(path[MinPoint]!=-1)
        {
            safePath.push(MinPoint);
            MinPoint=path[MinPoint];
        }
        safePath.push(MinPoint);
        while(!safePath.empty())
        {
            int Point=safePath.top();
            safePath.pop();
            cout<" "<int main()
{
    cin>>N>>D;
    for(int i=0;icin>>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;ireturn 0;
}

你可能感兴趣的:(zju数据结构MOOC习题)