07-图5 Saving James Bond - Hard Version

在电影《生死关头》中,詹姆斯邦德被毒贩困在一个布满鳄鱼的湖中小岛上,他必须跳跃到鳄鱼头上才能逃生。本篇通过分析问题,提出两种算法解决邦德如何找到最短逃生路径的问题。

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

解题思路

题目大意:James Bond位于湖中央(0,0)处直径为15的小岛上,湖的长宽分别为100,即|x|=50,|y|=50为湖的边界,输入D为跳跃的距离,N为周围鳄鱼的个数,接下来N行输入鳄鱼的坐标,James Bond可不断踩着鳄鱼的头进行跳跃,判断是否能到达岸边。若能到达岸边,则输出距离最短的路径,如果存在多条这样的路径,选择第一跳距离小岛最近的,否则输出0。
解法一:可以将第一跳的可到达的鳄鱼加入起点集,可到达岸边的鳄鱼加入终点集,那么问题转换为寻找起点集中的点与终点集中点的最短路径,多源最短路径问题可选择Floyd算法。将起点集按与小岛的距离升序排列,即可满足多个最短路径时的选取条件。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
const int MAXV = 110;
const int INF = 65535;
using namespace std;
struct point{            
    int x,y;
}p[MAXV];
const double RADIUS = 7.5; //半径 
int N,D;
int G[MAXV][MAXV],path[MAXV][MAXV];
vector<int> first,last;
double getDis(int i, int j){ //计算两点间距离
	return sqrt(pow(p[i].x - p[j].x, 2) + pow(p[i].y - p[j].y,2)); 
}
bool cmp(int i,int j){       //排序函数中的比较函数。把第一次能跳上的鳄鱼按照近小岛的距离升序排列 
    return getDis(i,0) < getDis(j,0);
}
bool isFirst(int i){ //第一跳能到达 
	return getDis(i,0) <= D + RADIUS;
}
bool isLast(int i){ //最后一跳能到达 
	return (abs(p[i].x) + D >= 50||abs(p[i].y) + D >= 50);
}
void getWay(int st, int ed){  //打印出经过的鳄鱼的坐标 
    printf("%d %d\n",p[st].x, p[st].y);
    while(st != ed){
        st = path[st][ed];
        printf("%d %d\n",p[st].x,p[st].y);
    }
}
void Floyd(){
	for(int k = 1; k <= N; k++){       
        for(int i=1; i <= N; i++){
            for(int j = 1; j <= N; j++){
                if(G[i][k] + G[k][j] < G[i][j]){
                    G[i][j] = G[i][k] + G[k][j];
                    path[i][j] = path[i][k];
                }
            }
        }
	}
}
int main()
{
	fill(G[0], G[0] + MAXV*MAXV, INF); //初始化图 
	for(int i = 1; i < MAXV; i++)
		for(int j = 1; j < MAXV; j++)
			path[i][j] = j; //初始化路径 
    cin>>N>>D; 
    if(D >= 50-RADIUS){             //一步到岸边 
        printf("1\n");
        return 0;
    }
    p[0].x = p[0].y = 0;        
    for(int i = 1; i <= N; i++){   //数组下标1开始存储数据 
        cin>>p[i].x>>p[i].y;
        if((getDis(i,0) <= RADIUS)||abs(p[i].x) >= 50||abs(p[i].y) >= 50) {   //当鳄鱼在岸上或在小岛上时,这条鳄鱼可以忽略不计。因此鳄鱼数量N减一,下条鳄鱼顶替它的数组中的位置 
            i--;
            N--;
        }   
    }
    for(int i = 1; i <= N; i++){
        for(int j = 1; j <= N; j++){
            if(getDis(i,j) <= D){
                if(i == j)   
                    G[i][j] = G[j][i] = 0;
                else
                    G[i][j] = G[j][i] = 1;                   
			}
        }   
    }
    for(int i=1;i<=N;i++)//加入起点集 
        if(isFirst(i))	
			first.push_back(i);  
    for(int i=1;i<=N;i++){ //加入终点集 
		if(isLast(i)) 
			last.push_back(i);
	}
	Floyd();
	sort(first.begin(), first.end(), cmp); //起点集按与小岛的距离从小到达排序 
    int minStep = INF, st, ed;    //分别为最小的跳跃数,起点下标,终点下标 
	for(int i=0; i < first.size(); i++){
    	int u = first[i];
        for(int j = 0; j < last.size(); j++){
        	int v = last[j];
            if(G[u][v] < minStep){
            	minStep = G[u][v];
            	st = u;
            	ed = v;
			}
        }
    }
    if(minStep != INF){ 
        printf("%d\n",minStep + 2); //minStep为起点到终点的距离,加上从小岛跳到起点鳄鱼,从终点鳄鱼跳到岸边的2步 
        getWay(st,ed);//打印路径 
    }else  //当最小的跳跃数为INF,则不可能逃脱 
        printf("0");
    return 0;
}

解法二:选取将小岛视为源点,编号为0;将岸边视为终点,编号为N+1,将它们加入图的顶点集中,则对于首跳能到达的鳄鱼i,创建边G[0][i], 对于能到达岸边的鳄鱼i,创建边G[j][N+1]。则问题转换为求从源点0到终点N+1的最短路径,单源最短路径问题采用Dijkstra算法。当存在多个最短路径时,按首跳的距离排序,因此创建start数组用于存放该节点最短路径的第一跳节点。当最短路径相同时,比较第一跳距离小岛的距离。注意:源点无法确定第一跳,因此在对路径的第一跳进行更新时,要求中介点u不为源点。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
const int MAXV = 110;
const int INF = 65535;
using namespace std;
struct point{            
    int x,y;
}p[MAXV];
const double RADIUS = 7.5; //半径 
int N,D;
int G[MAXV][MAXV];
int d[MAXV],pre[MAXV],start[MAXV]; //start用于记录该路径下的第一跳 
double getDis(int i, int j){ //计算两点间距离
	return sqrt(pow(p[i].x - p[j].x, 2) + pow(p[i].y - p[j].y,2)); 
}
bool closer(int i,int j){       //比较起点与小岛距离 
	return (pow(p[i].x, 2) + pow(p[i].y, 2)) < (pow(p[j].x, 2) + pow(p[j].y, 2));
}
bool isFirst(int i){ //第一跳能到达 
	return getDis(i,0) <= D + RADIUS;
}
bool isLast(int i){ //最后一跳能到达 
	return (abs(p[i].x) + D >= 50||abs(p[i].y) + D >= 50);
}
void Dijkstra(int s){
	fill(d, d + MAXV, INF);  //初始化整个d数组默认为INF 
	bool vis[MAXV] = {false};
	d[s] = 0;
	for(int i = 0; i <= N+1; i++){
		int u = -1, min = INF; //u使d[u]最小,min存放最小的d[u]
		for(int j = 0; j <= N+1; j++){
			if(!vis[j] && d[j] < min){
				u = j;
				min = d[j];
			}
		}
		//找不到小于INF的d[u],则说明剩下的点不连通
		if(u == -1) return;
		vis[u] = true;
		for(int v = 0; v <= N+1; v++){ //将u作为中介点,更新v 
			if(!vis[v] && G[u][v] != INF){
				if(d[u] + G[u][v] < d[v]){ //以u为中介点使d[v]更小
					 d[v] = d[u] + G[u][v];
					 pre[v] = u;
					 if(u != s) //如果u为源点,则不更新该路径下的第一跳,因为源点无法确定第一跳 
					 	start[v] = start[u]; 
				}else if(d[u] + G[u][v] == d[v]){  
					if(closer(start[u],start[v])){ //最短路径长度相同时,若u为中介的路径的第一跳距离小岛更近,则选择该路径 
						start[v] = start[u];
						pre[v] = u;
						if(u != s)  
							start[v] = start[u]; 
					}
				}
			}
		}
	}  
}
void DFS(int v){
	if(pre[v] == 0){//为第一跳 
		printf("%d %d\n",p[v].x,p[v].y);
		return;
	}
	DFS(pre[v]);
	if(v != N + 1)
		printf("%d %d\n",p[v].x,p[v].y);
}
int main()
{
	fill(G[0], G[0] + MAXV*MAXV, INF); //初始化图 
    cin>>N>>D; 
    if(D >= 50-RADIUS){             //一步到岸边 
        printf("1\n");
        return 0;
    }
    p[0].x = p[0].y = 0;        
    for(int i = 1; i <= N; i++){   //数组下标1开始存储数据 
        cin>>p[i].x>>p[i].y;
        if((getDis(i,0) <= RADIUS)||abs(p[i].x) >= 50||abs(p[i].y) >= 50) {   //当鳄鱼在岸上或在小岛上时,这条鳄鱼可以忽略不计。因此鳄鱼数量N减一,下条鳄鱼顶替它的数组中的位置 
            i--;
            N--;
        }   
    }
    for(int i = 1; i <= N; i++){
        for(int j = 1; j <= N; j++){
            if(getDis(i,j) <= D){
                if(i == j)   
                    G[i][j] = G[j][i] = 0;
                else
                    G[i][j] = G[j][i] = 1;                   
			}
        }   
    }
    for(int i = 1; i <= N; i++)
        if(isFirst(i)){
        	G[0][i] = 1;
        	start[i] = i; //第一跳能到达的节点的第一跳为自身 
		}	
    for(int i = 1; i <= N; i++)
		if(isLast(i)) 
			G[i][N+1] = 1;
	//计算从0到N+1的最短路径 
	Dijkstra(0);
    int tmp,minStep = d[N+1];    //分别为最小的跳跃数,起点下标,终点下标 
    if(minStep != INF){ 
        printf("%d\n",minStep); //minStep为起点到终点的距离,加上从小岛跳到起点鳄鱼,从终点鳄鱼跳到岸边的2步 
        DFS(N+1); //打印路径 
    }else  //当最小的跳跃数为INF,则不可能逃脱 
        printf("0");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值