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 NN (\le 100100), the number of crocodiles, and DD, the maximum distance that James could jump. Then NN lines follow, each containing the (x, y)(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)(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

这道题相比之前的拯救007来说,具体有以下变动:
1.输出最短跳数逃生方法;
2.要输出跳数,和踩着的鳄鱼的节点,如果不存在,输出0;
3.如果有多个跳数一样的,输出第一跳距离最短的。

所以考虑用变量:
cnt来计算有多少条路径;
first[]矩阵记录每条路径第一跳的大小。

在简单版本中,利用深度优先或者广度优先都能进行判断,但若要输出最短的逃出路径,深度优先显然不合适了,

因为它是一条道走到黑的…

#include<stdio.h>
#include<stdlib.h>
#include<math.h> 


#define MAXN 150 //最多150个点 
int dist[MAXN] ;//记录s到w的最短距离 
int cnt = 0;//计算有多少条路径 
int first[MAXN] = {0};//记录每条路径第一跳的大小
int trans_temp = 0;//传送成功了的跳数的第一跳大小 
int jump_path[MAXN] ;//记录每种路径最后一跳的节点 
int path[MAXN];//路径中经过的节点 
int trans_i = 0;
 
//建立邻接矩阵 
struct port{
	int x;
	int y;
}G[MAXN];//坐标 

int NV;//节点数 
int distance;//007能跳的距离

bool jump(int v1,int v2);//计算007一跳能不能成功
bool issafe(int v);//从v点能否跳上岸
bool Unweighted(int v);//无权图单源最短路算法 
bool FirstJump(int v);//第一跳的可能
void save007_hard();//007 主程序
void initialzation();//初始化模块 


int main()
{
	//freopen("in.txt","r",stdin);

	int i;
	int x,y;
	scanf("%d %d",&NV,&distance);//输入的是鳄鱼数
	
	if(distance >= 50-7.5)
	{
		printf("1\n");
		return 0;
	}
	
	G[0].x = G[0].y = 0;//孤岛
	 
	int j = 0;//用来计算在岸上和在岛上的鳄鱼 
	
	for( i=1; i<=NV; i++) //令孤岛为0节点 
	{
		scanf("%d %d",&x,&y);//读入坐标	
		
		if( (7.5*7.5) >= (x*x+y*y) )//鳄鱼在岛上 
		{
			j++;
			i--;
			NV--;
		}
		else if( (abs(x) >= 50 ) || ( abs(y) >= 50 ) )//鳄鱼在岸上 
		{
			j++;
			i--;
			NV--;
		}
		else
		{
			G[i].x = x;
			G[i].y = y; 
		} 


	}
	initialzation();
	save007_hard();
	
	//freopen("out.txt","w",stdout);
	return 0;
} 
//初始化
void initialzation()
{
	int i;
	for( i=0; i<MAXN; i++)
	{
		dist[i] = -1;//记录s到w的最短距离 
	 	jump_path[i] = -1;//记录每种路径最后一跳的节点 
		path[i]  = -2;//路径中经过的节点 
	} 
} 

//计算007一条能不能成功,排除第一跳和最后一跳 
bool jump(int v1,int v2)
{
	int x,y,d; 
	bool answer;
	if(v1 == 0)
	{
		answer = FirstJump(v2);
	}
	else
	{
		x = abs(G[v1].x - G[v2].x);
		y = abs(G[v1].y - G[v2].y);
		d = pow(x,2) + pow(y,2);//求两点的距离 
		
		if( d <= pow(distance,2) )
		{
			answer = true;
		}
		else
		{
			answer = false;
		}		
	}

	return answer;
}

//从v点能否跳上岸 
bool issafe(int v)
{
	int x,y;
	bool answer;
	
	x = abs(G[v].x);
	y = abs(G[v].y);
	
	
	if( (x+distance >= 50) || (y+distance >= 50))
	{
		answer = true;
	}
	else
	{
		answer = false;
	}
	
	return answer;
}

无权图单源最短路算法,针对007这道题 
bool Unweighted(int v)
{
	const int MAXNUM = 200;
	int queue[MAXNUM] = {0};
	int first1 = -1,rear = -1;
	
	queue[++rear] = v;//入队
	
	bool answer = false;//最后一跳节点 


	while(first1 < rear)
	{	
		int de = queue[++first1];//弹出节点 
		
			
		if(issafe(de))//如果此节点可以跳到岸上 
		{
			jump_path[cnt] = de; 
			cnt++;//路径多了一个 
			answer = true;
		} 
		else//否则,访问de的所有邻接点 
		{
			int w;
			for( w=1; w<=NV; w++)
			{		
				if( dist[w] == -1 && jump(de,w) )//de没有被访问过且可以跳过去 
				{
					trans_i = w;//记录每个节点第一跳的长度 
					first[trans_i] = trans_temp; 
					dist[w] = dist[de] +1;//跳数增加 
					path[w] = de;//路径记录 
					queue[++rear] = w;	
				}
			}
	
		} 
	} 

	return answer; 
} 


//第一跳的可能 
bool FirstJump(int v)
{
	int x,y;
	double d;
	bool answer;
	
	
	x = abs(G[v].x);
	y = abs(G[v].y);//用绝对值 
	d =sqrt(pow(x,2) + pow(y,2));
	
	if( d <= (distance+7.5) )//小岛半径7.5,直径15 
	{
		trans_temp = d;  //记录第一跳的距离 
		answer = true;
	}
	else
	{
		answer = false;
	}
	
	return answer; 
}
//007 主程序
void save007_hard()
{
	int i,node;
	
	dist[0] = 0;//设孤岛为0节点,跳数为0 
	
	node = Unweighted(0);//输出最后一个节点
		

	if(node == true)//说明有节点可以跳到岸上 
	{
		//简单建立一个堆栈 
		int stack[100] = {0},top = 0;
		int min_dist,dist_index;
		if(cnt != 1)
		{
		//判断多个最短路径跳数
			min_dist = dist[jump_path[0]];
			dist_index = 0;
			
			for( i=1; i<cnt; i++)
			{
				if(dist[jump_path[i]] < min_dist)
				{
					dist_index = i;
				}
			}
		}
		//判断同跳数的第一跳大小 
//		int index,min_first;
//		if(cnt != 1)
//		{
//			
//			min_first = first[0];
//			index = 0;
//			for(int i=1; i<cnt; i++)
//			{
//				if(first[i] < min_first);
//				{
//					min_first = first[i];
//					index = i;
//				}
//			} 
//		} 
		node = jump_path[dist_index];
		//输出最短路径
		printf("%d\n",dist[node]+1);//输出有几跳 ,+1算上跳到岸上 
		while(node != -2) //压栈 
		{
			stack[top++] = node;
			node = path[node];
		}
		while(top)
		{
			int output = stack[--top];
			if(output != 0)
			{
				printf("%d %d\n",G[output].x,G[output].y);
			}
		} 
		
	}
	else
	{
		printf("0\n");
	}
} 


目前测试点4和测试点5通不过,测试点5我知道是相同跳数,第一跳最短的情况,但是没想好怎么处理

测试点4感觉处理了,但是通不过,不知道哪里没考虑到,先放到这里





  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值