07-图5 Saving James Bond - Hard Version

该博客讲述了如何通过算法解决电影《生死关头》中詹姆斯·邦德在鳄鱼湖中逃生的情节。给定鳄鱼的位置和邦德的最大跳跃距离,需要找出邦德到达湖岸的最短跳跃路径。博客详细介绍了如何构建无向图,使用邻接表存储,并应用无权图的单源最短路算法来求解问题。如果存在可行路径,输出最小跳跃次数和路径;否则,输出0表示无法逃脱。
摘要由CSDN通过智能技术生成

 

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

思路:

1:根据各个鳄鱼坐标以及跳跃距离的关系建立无向图,用邻接表形式存储;

(邻接表结构定义,CreatG函数,InsertEdge函数,BuiltG函数;

    Location结构数组存储鳄鱼坐标;

    CanJump函数判断两节点是否存在边;)

2:无权图的单源最短路算法,初始将符合FirstJump的结点入队,更新对应结点的Dist为1;

 (Dist[]数组所有元素初始化为-1,在每一个结点入队时对该节点进行更新;

     Path[]数组存储路径;

     队列的结构定义,CreatQ函数,AddQ函数,DeleteQ函数,IsEmptyQ函数;

     Issafe函数对出队结点判断能否跳出鳄鱼池;)

 3:输出最终符合Issafe函数的节点的Dist值,再使用堆栈逆序输出路径;

(PrintPath函数;

     栈的结构定义,Push函数,Pop函数,IsEmptyS函数;}

#include<stdio.h>
#include<stdlib.h>
#define DataType int
#define Vertex int
#define Maxsize 100
#define f(x) x*x

typedef struct Location{
	double x;
	double y;
}Loc;
Loc Location[Maxsize];

typedef struct AdjVNode *PtrToAdjVNode;
struct AdjVNode{
	Vertex AdjV;
	PtrToAdjVNode Next;
};
typedef struct VNode{
	PtrToAdjVNode FirstEdge;
}AdjList[Maxsize];

typedef struct GNode *LGraph;
struct GNode{
	int Nv;
	int Ne;
	AdjList LG;
};

typedef struct QNode *Queue;
struct QNode{
	int Size;
	int front;
	int rear;
	Vertex *Q;
};

typedef struct SNode *Stack;
struct SNode{
	int top;
	Vertex *S;
};

int Dist[Maxsize]={};
Vertex Path[Maxsize];

LGraph BuiltG(int N,int D);
int CanJump(Vertex V2,Vertex V1,int D);
LGraph CreatG(int N);
void InsertEdge(LGraph G,Vertex V1,Vertex V2);
void Save007(LGraph G,int D);
int FirstJump(Vertex V,int D);
int Issafe(Vertex V,int D);
void ResetP(Vertex Path[]);
void PrintPath(Vertex Path[],Vertex V);
Stack CreatStack();
void Push(Stack S,Vertex V);
Vertex Pop(Stack S);
int IsEmptyS(Stack S);
Queue CreatQ(int N);
void AddQ(Queue Q,Vertex V);
Vertex DeleteQ(Queue Q);
int IsEmptyQ(Queue Q);
int FirstJump(Vertex V,int D);
int Issafe(Vertex V,int D);

int main()
{
	int N,D;
	Vertex i;
	LGraph G;
	scanf("%d %d",&N,&D);
	for(i=0;i<N;i++)
		scanf("%lf %lf",&Location[i].x ,&Location[i].y);
	G=BuiltG(N,D);
	Save007(G,D);
	return 0;
}

LGraph BuiltG(int N,int D)
{
	Vertex i,j;
	LGraph G;
	G=CreatG(N);
	for(i=0;i<N;i++)
	{
		for(j=i+1;j<N;j++)
		{
			if(CanJump(i,j,D))
			{
				InsertEdge(G,i,j);
//				printf("Edge: %d %d\n",i,j);
			}	
		}
	}
	return G;
}

int CanJump(Vertex V2,Vertex V1,int D)
{
	int Distance,X,Y;
	X=Location[V1].x-Location[V2].x;
	Y=Location[V1].y -Location[V2].y;
	Distance=f(X)+f(Y);
	if(Distance<=f(D))
		return 1;
	else 
		return 0;
}

LGraph CreatG(int N)
{
	LGraph G=(LGraph)malloc(sizeof(struct GNode));
	G->Ne=0;
	G->Nv=N;
	int V;
	for(V=0;V<N;V++)
		G->LG[V].FirstEdge =NULL;
	return G;
}

void InsertEdge(LGraph G,Vertex V1,Vertex V2)
{
	PtrToAdjVNode NewNode;
	
	NewNode=(PtrToAdjVNode)malloc(sizeof(struct AdjVNode));
	NewNode->AdjV=V2;
	NewNode->Next=G->LG[V1].FirstEdge ;
	G->LG[V1].FirstEdge=NewNode;
	
	NewNode=(PtrToAdjVNode)malloc(sizeof(struct AdjVNode));
	NewNode->AdjV=V1;
	NewNode->Next=G->LG[V2].FirstEdge ;
	G->LG[V2].FirstEdge=NewNode;
}

void Save007(LGraph G,int D)
{
	Queue Q;
	Vertex V;
	int answer=0;
	Q=CreatQ(G->Nv);
	ResetP(Path);
	
	for(V=0;V<G->Nv;V++)
		if(FirstJump(V,D))
		{
			AddQ(Q,V);
			Dist[V]=1;
		}
	while(!IsEmptyQ(Q))
	{
		V=DeleteQ(Q);
		if(Issafe(V,D))
		{
			answer=1;
			break;
		}	
		PtrToAdjVNode p;
		p=G->LG[V].FirstEdge;
		while( p )
		{
			if(!Dist[p->AdjV])
			{
				Dist[p->AdjV]=Dist[V]+1;
				Path[p->AdjV]=V;
				AddQ(Q,p->AdjV);
			}
			p=p->Next;
		}
	}
	if(answer==1)
	{
		printf("%d\n",Dist[V]+1);
		PrintPath(Path,V);
	}
	else
		printf("0\n");
}

void ResetP(Vertex Path[])
{
	int V;
	for(V=0;V<Maxsize;V++)
	{
		Path[V]=-1;
	}
}

void PrintPath(Vertex Path[],Vertex V)
{
	Stack S;
	S=CreatStack();
	while(V!=-1)
	{
		Push(S,V);
//		printf("%d \n",V);
		V=Path[V];
	}
	while(!IsEmptyS(S))
	{
		V=Pop(S);
		printf("%g %g\n",Location[V].x,Location[V].y);
	}
}

Stack CreatStack()
{
	Stack S=(Stack)malloc(sizeof(struct SNode));
	S->S=(Vertex *)malloc(Maxsize*sizeof(Vertex));
	S->top=-1;
	return S;
}

void Push(Stack S,Vertex V)
{
	S->S[++S->top]=V;
}

Vertex Pop(Stack S)
{
	return S->S[S->top--];
}

int IsEmptyS(Stack S)
{
	return S->top==-1;
}

Queue CreatQ(int N)
{
	Queue Q=(Queue)malloc(sizeof(struct QNode));
	Q->Q=(Vertex *)malloc(N*sizeof(Vertex));
	Q->Size=N;
	Q->front=Q->rear=-1;
	return Q;
}

void AddQ(Queue Q,Vertex V)
{
	Q->Q[(++Q->rear)%Q->Size]=V;
}

Vertex DeleteQ(Queue Q)
{
	return Q->Q[(++Q->front)%Q->Size];
}

int IsEmptyQ(Queue Q)
{
	return Q->front==Q->rear;
}

int FirstJump(Vertex V,int D)
{
	return f(Location[V].x)+f(Location[V].y)-f((D+7.5))<=0;
}

int Issafe(Vertex V,int D)
{
	if((50-Location[V].x<=D)||(50-Location[V].y<=D)||(Location[V].x+50<=D)||(Location[V].y+50<=D))
		return 1;
	else 
		return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值