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

思路:

用结构体记录每个鳄鱼坐标、需要跳的次数、前一个鳄鱼下表以及第一跳距离。

用一个vector记录第一次跳能到的鳄鱼,将这些鳄鱼的次数更新为1 。

然后不断在不能到的鳄鱼与vector里面的鳄鱼之间计算距离,更新vector和次数和pre

最后在可以跳出去的鳄鱼之间排序,输出第一跳最小的

#include <bits/stdc++.h>
using namespace std;
int n,d;
struct node{
	int x,y;
	int vis,pre,now;
	double first;
};
node l[105];
void print(int t){
	if(l[t].pre == t){
		cout<<l[t].x - 50 <<" "<<l[t].y - 50 <<endl;
	}else {
		print(l[t].pre );
		cout<<l[t].x - 50 <<" "<<l[t].y - 50 <<endl;
	}
}
bool cmp(node a,node b){
	if(a.vis == b.vis ){
		return a.first < b.first ;
	}else return a.vis < b.vis ;
}
int main()
{
	cin>>n>>d;
	for(int i =1;i<=n;i++){
		int a,b;
		cin>>a>>b;
		l[i].x = a+50;
		l[i].y = b+50;
		l[i].vis = 0;
		l[i].pre = i;
		l[i].now = i;
	}
	vector<node>c;				//c存储可以跳到的鳄鱼,并记录需要几跳和第一次跳的距离 
	for(int i = 1;i<=n;i++){
		double dis = sqrt( (l[i].x-50) *(l[i].x-50 )+(l[i].y -50)*(l[i].y-50) );
		if(dis <= 7.5 + d && dis > 7.5){
			l[i].vis = 1;
			l[i].first = dis - 7.5;
			c.push_back(l[i]);
		}
	}
	int cnt = c.size();
	int ccnt = 0;
	while(ccnt < cnt){					//当有新的可达鳄鱼时继续循环 
		for(int i = 1;i<=n;i++){
			if(l[i].vis )continue;
			int flag = 0;
			int fir = 200;
			int pr;
			for(int j = 0;j<cnt;j++){//计算可达的鳄鱼与不可达的鳄鱼距离,将dis<=d的加入c,并将第一跳最小的记为pre 
				double dis = sqrt((l[i].x -c[j].x)*(l[i].x -c[j].x)+(l[i].y -c[j].y)*(l[i].y -c[j].y));
				if(dis <= d){
					flag = 1; 
					l[i].vis = c[j].vis + 1;//需要多一跳 
					if(c[j].first < fir){	//可能c中有多个鳄鱼可达当前鳄鱼,只记录first最小的 
						fir = c[j].first;
						pr = c[j].now;
					}
				}
			}
			if(flag){
				l[i].pre = pr;
				l[i].first = fir;
				c.push_back(l[i]);
			}
		}
		ccnt = cnt;
		cnt = c.size();
	}
	int flag = 0;
	if(d + 7.5 >= 50)flag = 1;	//不用跳鳄鱼 
	vector<node>cc;			//存储下一次可跳出去的鳄鱼 
	for(int i = 0;i<cnt;i++){
		int fff = 0;
		if(c[i].x +d>=100)fff = 1;
		if(c[i].x-d<=0)fff = 1;
		if(c[i].y+d>=100)fff = 1;
		if(c[i].y-d<=0)fff = 1;
		if(fff){
			flag = 1;
			cc.push_back(c[i]);
		}
	}
	if(flag){
		if(d + 7.5 >=50){
			cout<<"1";
			return 0;
		}
		sort(cc.begin(),cc.end(),cmp);
		cout<<cc[0].vis + 1<<endl;
		print(cc[0].now);
	}
	else cout<<"0";
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值