菜鸡奋斗路07-图5 Saving James Bond - Hard Version

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
作者: 陈越
单位: 浙江大学
时间限制: 400ms
内存限制: 64MB
代码长度限制: 16KB


个人分析:第二次拯救007了,这次不仅要判断他是否能安全到岸,还要帮他确定最短路线。首先,将第一跳能跳到的几个结点看作首结点集。每个首结点进行BFS,记录安全到岸所需的跳跃数(路长),从中先选取跳跃数最短的首结点路径,若有俩条路的路长相等,则取首结点最近(离中心岛)的那条路径。

图+队列+BFS,就能做出这道题。


代码如下:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define Maxsize 100
#define infinity 65535
typedef int Vertex;
typedef int WeightType;
int Visited[Maxsize]={0};
int x[Maxsize],y[Maxsize];

typedef struct GNode *MGraph;
struct GNode{
	int Nv;
	int Ne;
	int Djump;
	WeightType G[Maxsize][Maxsize]; 
};
bool Jump(int Djump,int n,int k)
{
	int Xdistance=x[n]-x[k];
	int Ydistance=y[n]-y[k];
	int Distance=Xdistance*Xdistance+Ydistance*Ydistance;
	if(Distance<=Djump*Djump)
		return true;
	else
		return false;
}
MGraph CreateGraph(int Nv,int Djump)
{
	MGraph Graph;
	Graph=(MGraph)malloc(sizeof(GNode));
	Graph->Nv=Nv;
	Graph->Djump=Djump;
	return Graph;
}

MGraph BuildGraph(int Nv,int Djump)
{
	MGraph Graph;
	int i,j;int Ne=0;
	Graph=CreateGraph(Nv,Djump);
	for(i=0;i<Nv;i++)
	{
		for(j=0;j<Nv;j++)
		{
			if(i==j)
			{
				Graph->G[i][j]=infinity;
			}
			else
			{
				if(Jump(Djump,i,j))
				{
					Graph->G[i][j]=1;
					Ne++;
				}
				else
					Graph->G[i][j]=infinity;
			}
		}
	}
	Graph->Ne=Ne;
	return Graph;
} 

typedef int Position;
struct QNode {
    int *Data;     /* 存储元素的数组 */
    int Front, Rear;  /* 队列的头、尾指针 */
    int MaxSize;           /* 队列最大容量 */
};
typedef struct QNode *Queue;
 
Queue CreateQueue( int MaxSize )
{
    Queue Q = (Queue)malloc(sizeof(struct QNode));
    Q->Data = (int *)malloc(MaxSize * sizeof(int));
    Q->Front = Q->Rear = 0;
    Q->MaxSize = Maxsize;
    return Q;
}
 
bool IsFull( Queue Q )
{
    return ((Q->Rear+1)%Q->MaxSize == Q->Front);
}
 
bool AddQ( Queue Q, int X )
{
    if ( IsFull(Q) ) {
        printf("队列满");
        return false;
    }
    else {
        Q->Rear = (Q->Rear+1)%Q->MaxSize;
        Q->Data[Q->Rear] = X;
        return true;
    }
}
 
bool IsEmpty( Queue Q )
{
    return (Q->Front == Q->Rear);
}
 
int DeleteQ( Queue Q )
{
    if ( IsEmpty(Q) ) { 
        printf("队列空");
        return -1;
    }
    else  {
        Q->Front =(Q->Front+1)%Q->MaxSize;
        return  Q->Data[Q->Front];
    }
}

bool IsSafe(MGraph Graph,int n)
{	
	if((Graph->Djump-50)>=x[n]||x[n]>=(50-Graph->Djump))
		return true;
	else if((Graph->Djump-50)>=y[n]||y[n]>=(50-Graph->Djump))
		return true;
	else
		return false;
}

bool FirstJump(MGraph Graph,int n)
{	
	int Xdistance=x[n];
	int Ydistance=y[n];
	int Distance=Xdistance*Xdistance+Ydistance*Ydistance;
	if(Distance<=(Graph->Djump+7.5)*(Graph->Djump+7.5)&&Distance>7.5*7.5)
		return true;
	else
		return false;
}

bool IsEdge( MGraph Graph, Vertex V, Vertex W )
{
    return Graph->G[V][W]<infinity ? true : false;
}
/* Visited[]为全局变量,已经初始化为0 */
int BFS ( MGraph Graph, Vertex S,int path[],int *End)
{   /* 以S为出发点对邻接矩阵存储的图Graph进行BFS搜索 */
    Queue Q;     
    Vertex V, W;
    int i=0;int count=1;int tail;int last=S;
 
    Q = CreateQueue( Maxsize ); //创建空队列, MaxSize为外部定义的常数 

    Visited[S] = 1; // 标记S已访问 
    AddQ(Q, S); // S入队列 

    while ( !IsEmpty(Q) ) 
	{
        V = DeleteQ(Q); 
    	
        if(IsSafe(Graph,V))
        {
        	*End=V;
			return count;    //当能到岸时,返回跳跃次数(路长)
		}
        else
        {
        	for( W=0; W<Graph->Nv; W++ )
			{
			 // 若W是V的邻接点并且未访问过
            	if ( !Visited[W] && IsEdge(Graph, V, W) ) {
                /* 访问顶点W */
               		Visited[W] = 1; // 标记W已访问 
                	AddQ(Q, W); // W入队列 
                	tail=W;
                	path[W]=V;
            	}
			} 
            if(last==V)
            {
            	count++;
            	last=tail;
			}
		}
    } 
    return infinity;   //当无法到岸时,返回infinity
}

void PrintQueue(int path[],int End)
{
	if(path[End]==infinity)
		printf("%d %d\n",x[End],y[End]);
	else
	{
		PrintQueue(path,path[End]);
		printf("%d %d\n",x[End],y[End]);
	}
}

void Save007(MGraph Graph)
{	
	int Minhead=0;int MinD=infinity;
	int Minnum=infinity;int num=infinity;int end;int Realend;   
	//Minnum存储全局最短路长,num存储当前最短路长,Realend全局最短路的尾结点,end当前最短路的尾结点
	int path[Maxsize]={0};int Realpath[Maxsize]={0};  
	for(int i=0;i<Graph->Nv;i++)
	{
		path[i]=infinity;
		Realpath[i]=infinity;
	}
	if(Graph->Djump+7.5>50)         //从中心岛直接能跳到岸
	{
		printf("1\n");
		return;	
	}
	for(int k=0;k<Graph->Nv;k++)
	{	
		if(FirstJump(Graph,k))          //选出第一跳能到达的首结点
		{	
			Minhead=k;
			MinD=sqrt(x[k]*x[k]+y[k]*y[k])-7.5;
			for(int j=k+1;j<Graph->Nv;j++)    //选出最近的首结点
			{	
				if(FirstJump(Graph,j))
				{
					if(sqrt(x[j]*x[j]+y[j]*y[j])-7.5<MinD)
					{
						Minhead=j;
						MinD=sqrt(x[j]*x[j]+y[j]*y[j])-7.5;
					}
				}	
			}
			num=BFS(Graph,Minhead,path,&end);    //BFS,判断从该首结点出发能否到岸
			for(int j=0;j<Graph->Nv;j++)
			{
				Visited[j]=0;
			}
			if(Minnum>num)
			{
				Minnum=num;
				Realend=end;
				for(int i=0;i<Graph->Nv;i++)
					Realpath[i]=path[i];
				for(int i=0;i<Graph->Nv;i++)
					path[i]=infinity;		
			}
			else
			{
				for(int i=0;i<Graph->Nv;i++)
					path[i]=infinity;
			}
			
		}
	}
	if(Minnum==infinity)
		printf("0\n");
	else
	{
		printf("%d\n",Minnum+1);    //加上第一跳
		PrintQueue(Realpath,Realend);
	} 
	
}

int main()
{	//以x[]y[]存储所有鳄鱼的位置
	int number,jumpdistance,i,j,k;
	scanf("%d %d",&number,&jumpdistance);	
	getchar();
	for(i=0;i<number;i++)
		scanf("%d %d",&x[i],&y[i]);
	MGraph Graph;
	Graph=BuildGraph(number,jumpdistance);
	//将头结点按距离大小排序

	//拯救007
	Save007(Graph); 
	return 0;
} 

运行结果:


总结:

这题让菜鸡把一些方面的知识统一用了起来,巩固熟悉了队列/图结构体/BFS等的基本操作。同时,又有一定的实际应用背景,虽然题目很魔幻,007踩着鳄鱼头跳上岸,但如果换成是多个城市间商品的运输路线/能量传输路线等等,就会有非常可观的实际价值。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值