07-图5 Saving James Bond - Hard Version(30 分)

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

写了一半,想看看找出最短路径是否正确 ,出错了,由于时间紧张,留坑等我有时间再回来看

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#define MaxVertexNum 101
//#define INFINITY 65535

typedef int Vertex;
typedef int WeightType;


struct DataType{
    int x;
    int y;
    //bool Visited;
}Data[MaxVertexNum];
int Nv,D;

int dist[MaxVertexNum]={-1};
Vertex path[MaxVertexNum]={-1};

int CalcuDistance(Vertex V1,Vertex V2){
    return sqrt( pow(Data[V1].x-Data[V2].x,2)+pow(Data[V1].y-Data[V2].y,2) );
}

bool IsSafe(Vertex V){
    int x,y;
    x=Data[V].x;y=Data[V].y;
    if(x+50<=D || 50-x<=D || y+50<=D || 50-y<=D)    return true;
    else    return false;
}

//队列的节点
typedef Vertex ElementType;
struct QNode{
    ElementType Element;
    struct QNode * Next;
};
//队列的空头节点,含有指向头尾的指针
struct HeaderQNode{
    struct QNode * Front;
    struct QNode * Rear;
};
typedef struct HeaderQNode * Queue;




//Breadth First Search
//以下为队列
struct HeaderQNode * CreateQueue(void)
{
    struct HeaderQNode * p;
    p=(struct HeaderQNode *)malloc(sizeof(struct HeaderQNode));
    p->Front=NULL;p->Rear=NULL;
    return p;
}
bool IsEmpty(struct HeaderQNode * p)
{
    if(p->Rear==NULL)   return true;
    else    return false;
}
void EnQueue(struct HeaderQNode * p,ElementType V)
{
    struct QNode * temp=(struct QNode *)malloc(sizeof(struct QNode));temp->Element=V;
    if(p->Rear==NULL){
        p->Front=temp;
        temp->Next=NULL;
        p->Rear=temp;
    }else {
        p->Rear->Next=temp;
        temp->Next=NULL;
        p->Rear=temp;
    }
}
ElementType DeQueue(struct HeaderQNode * p)
{
    ElementType ret;
    if(!IsEmpty(p)){
        struct QNode * temp=p->Front;
        ret=temp->Element;
        p->Front=temp->Next;
        if(p->Front==NULL)  p->Rear=NULL;
        free(temp);
    }else   return 0;
    return ret;
}//队列定义结束
//BFS的变形
int Unweighted(Vertex S){
    Queue Q=CreateQueue();
    dist[S]=0;
    EnQueue(Q,S);
    Vertex V,W;
    while(!IsEmpty(Q)){
        V=DeQueue(Q);
        if(IsSafe(W))
            return dist[W];
        for(W=0;W<Nv;W++){
            if(CalcuDistance(V,W)<=D && dist[W]==-1){
                dist[W]=dist[V]+1;
                path[W]=V;
//                if(IsSafe(W))
//                    return dist[W];
                EnQueue(Q,W);
            }
        }
    }
    return -3;
}

int main()
{
    int i,answer,Min=1000;
    scanf("%d %d",&Nv,&D);
    for(i=0;i<Nv;i++){
        scanf("%d %d",&Data[i].x,&Data[i].y);
    }

    Data[Nv].x=0;Data[Nv].y=0;//设为center
    for(i=0;i<Nv;i++){
        if(CalcuDistance(Nv,i)<=D+7.5 && dist[i]==-1){
            dist[i]=0;
            answer=Unweighted(i);answer+=2;
            if(answer>0 && Min>answer)  Min=answer;
        }
    }

    if(Min<1000)    printf("%d\n",Min);
    else    printf("0");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值