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
###思路:
1.基于easy version改编来的,先以easy version的判断方法判断是否可以上岸
2.然后以dijstra算法求单源最短路,并记录路径。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<queue>
#include<cstring>
#include<stack>
#include<vector>
#include<algorithm>
using namespace std; 
/*
1.将007、岸边、鳄鱼看作n+2个节点,
2.从0出发,寻找0--n+1的最短路 
*/
const int MAX_N = 105;//最大规模 
int path[MAX_N]; //记录跳跃路径 path[a] = b,代表a的前一个跳跃点是b 
int g[MAX_N][MAX_N];
int visited[MAX_N];
queue<int> q;
int dist[MAX_N];

struct location{
	int x;
	int y;
}loc[MAX_N*MAX_N];


inline float dis(struct location a, struct location b){
	return pow((pow(a.x-b.x, 2)+pow(a.y-b.y, 2)),0.5);
}
void show(int n){
	for(int i = 0; i <= n+1; i++){
		for(int j = 0; j <= n+1; j++){
			cout<<g[i][j]<<" ";
		}
		cout<<endl;
	}
}
bool DFS(int cur, int n){
	if(cur==n+1)
		return true;
	visited[cur]=1;
	int i;
	bool flag=false;
	for(i = 1;i<n+2;i++){
		if(visited[i]==0&&g[cur][i]==1){
			flag = DFS(i,n);
			break;
		}
	}
	if(i==n+2)
		return false;
	else if(flag){
		return true;
	}else{
		DFS(cur,n);
	}	
} 
bool cmp(int a, int b){
	if(dis(loc[a],loc[0])>dis(loc[b],loc[0]))
		return true;
	return false;
}
void djstl(int n){
	vector<int>tmp;
	dist[0]=0;
	int cur = 0;
	memset(visited,0,sizeof(visited));
	for(int i=1;i<=n+1;i++){
		if(g[0][i]==1){
			visited[i]=1;
			tmp.push_back(i);
			path[i]=0;
			dist[i]=1;
		} 
	} 
	sort(tmp.begin(),tmp.end(),cmp);
	while(!tmp.empty()){
		q.push(tmp.back());
		tmp.pop_back();
	}
	while(!q.empty()){
		cur=q.front();
		q.pop();
		visited[cur]=1;
		//if(cur==n+1)
		//	break;
		for(int i=1;i<=n+1;i++){
			if(visited[i]==0&&g[cur][i]==1){
				q.push(i);
				if(dist[i]>dist[cur]+1){
					path[i]=cur;
					dist[i]=dist[cur]+1; 
				}
			}
		}
	}
}
void init(int n, int d){
	//建图,图中0代表007,1-n代表鳄鱼,n+1代表岸边 
	for(int i = 1; i <= n; i++){
		//建立鳄鱼和河岸的联系
		if(50-loc[i].x<=d || loc[i].x+50<=d||loc[i].y+50<=d||50-loc[i].y<=d){
			g[n+1][i] = 1;
			g[i][n+1] = 1;
		} 
	} 
	for(int i = 1; i <= n; i++){
		//建立007和鳄鱼的联系
		if(dis(loc[0],loc[i])-7.5<=d){
			g[0][i] = 1;
			g[i][0] = 1;
		} 
	} 
	//建立007和河岸的联系 
	if(50-7.5-d<=0){
		g[0][n+1]=1;
		g[n+1][0]=1;
	}
		//建立鳄鱼和鳄鱼的联系
	for(int i = 1; i <= n; i++){
		for(int j = i; j <= n; j++){
			if(dis(loc[i],loc[j])<=d||i==j){
				g[i][j]=1;
				g[j][i]=1;
			} 
		}
	} 
	
	
	//初始化path dist
	for(int i = 0; i <= n+1; i++){
		path[i]=-1;
		dist[i]=MAX_N;
	}
}
int main(){
	int n,d;
	cin>>n>>d;
	for(int i = 1; i <= n;i++){
		cin>>loc[i].x>>loc[i].y;
	}
	init(n,d);
	
	//show(n); 
	if(!DFS(0,n)){
		cout<<"0"<<endl;
		return 0;
	}
	//单源最短路,0--n+1的最短距离 
	djstl(n);
	cout<<dist[n+1]<<endl;
	int i = n+1;
	stack<int> stack;
	while(path[i]!=0){
		stack.push(path[i]);
		i=path[i];
	}
	while(!stack.empty()){
		int tmp = stack.top();
		stack.pop();
		cout<<loc[tmp].x<<" "<<loc[tmp].y<<endl;
	}
	return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值