图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.

假设这个湖是100×100平方的。假设湖心位于(0,0),东北角位于(50,50)。中心岛是一个以(0,0)为中心的圆盘,直径为15。许多鳄鱼在湖中的不同位置。考虑到每只鳄鱼的坐标和詹姆斯可以跳跃的距离,你必须告诉他一条最短的路径可以到达其中一个河岸。路径的长度是詹姆斯必须跳的次数。

输入规格:

每个输入文件包含一个测试用例。每种情况都以一行开始,该行包含两个正整数N(≤100)、鳄鱼数量和D(James可以跳跃的最大距离)。接下来是N行,每行包含鳄鱼的(x,y)位置。注意,没有两条鳄鱼停留在同一位置。

输出规格:

对于每个测试用例,如果James能够逃逸,则在一行中输出他必须跳转的最小次数。然后从下一行开始,输出路径上每只鳄鱼的位置(x,y),每对鳄鱼排成一行,从岛屿到河岸。如果詹姆斯不可能那样逃跑,只要给他0作为跳跃次数。如果有很多最短路径,只需输出第一跳最小的路径,这是唯一的

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我没有计算能一步跳到岸边的情况。

题目中加红线的地方我刚开始没注意到!!结果二十分钟写完了的程序调试了几十遍都出错!!!

具体思路就是把第一次能跳上的鳄鱼按照距离进行排序,然后不把源S进栈,而是改为把第一次能跳上去的鳄鱼依次进栈。

然后就是无权图的最小路径问题了。

#include <iostream>
#include <map>
#include <queue>
#include <vector>
#include <string>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
//0代表中心岛,最后一个代表岸边
int Graph[102][102];
int Cordlx[102];
int Cordly[102];
int dist[102];
int path[102];
int dista1[102];
queue<int> myQueue;
stack<int> myStack;
void initGraph(int N);
void generateGra(int E,int D);
void Unweighted(int N, int S);

int main(void) {
	int N,D;
	cin >> N >> D;
	initGraph(N);
	generateGra(N,D);

	Unweighted(N,0);

	system("pause");
	return 0;
}

void initGraph(int N) {
	for (int i = 0;i <= N+1;i++) {
		dist[i] = -1;
		for (int j = 0;j <= N+1;j++) {
			Graph[i][j] = -1;
			if (i == j) {
				Graph[i][j] = 1;
			}
		}	
	}
}

double pow2(double a) {
	return a*a;
}
double Max(double a, double b) {
	return a > b ? a : b;
}
double Min(double a, double b) {
	return a < b ? a : b;
}
void generateGra(int E,int D) {
	//E为鳄鱼的数量
	for (int i = 1;i <= E;i++) {
		int a, b;
		cin >> a >> b;
		Cordlx[i] = a;
		Cordly[i] = b;
	}
	//判断能不能从岛跳到鳄鱼背上
	for (int i = 1;i <= E;i++) {
		if (pow2(7.5 + D) >= pow2(Cordlx[i])+ pow2(Cordly[i])) {
			//能跳到该鳄鱼背上
			Graph[0][i] = 1;
			Graph[i][0] = 1;
		}
	}
	//判断能不能从一条鳄鱼跳到另一条鳄鱼
	for (int i = 1;i <= E;i++) {
		for (int j = i + 1;j <= E;j++) {
			if (pow2(D) >= (pow2(Cordlx[i]-Cordlx[j])+pow2(Cordly[i] - Cordly[j]))) {
				Graph[i][j] = 1;
				Graph[j][i] = 1;
			}
		}
	}
	//计算是否能上岸
	//能否一步上岸
	if (7.5+D>=50) {
		Graph[0][E + 1] = 1;
		Graph[E + 1][0] = 1;
	}
	for (int i = 1;i <= E;i++) {
		if (50 - Max(Cordlx[i], Cordly[i]) <= D || 50 + Min(Cordlx[i], Cordly[i]) <= D) {
			Graph[E + 1][i] = 1;
			Graph[i][E + 1] = 1;
		}
	}
	
	//for (int i = 0;i <= E+1;i++) {
	//	for (int j = 0;j <= E+1;j++) {
	//		cout << Graph[i][j] << " ";
	//	}
	//	cout << endl;
	//}

}
vector<pair<int, int>>myVector;
pair<int, int> myPair;
void distance(int N){
	for (int i = 1;i <= N+1;i++) {//必须是N+1,否则,如果为N,如果一次就能跳上岸则会出错
		//(因为后面会把第一次跳的结果进队列,而不是把最开始的S进队列)
		if (Graph[0][i] == 1) {//能跳到第i条鳄鱼身上
			myPair.first = Cordlx[i]*Cordlx[i]+Cordly[i]*Cordly[i];
			myPair.second = i;
			myVector.push_back(myPair);
		}
	}
	sort(myVector.begin(), myVector.end());
	//for (int i = 0;i < myVector.size();i++) {
		//cout << myVector[i].first << " " << myVector[i].second << endl;
	//}

}

void Unweighted(int N, int S)
{
	distance(N);
	for (int i = 0;i < myVector.size();i++) {
		myQueue.push(myVector[i].second);
		dist[myVector[i].second] = 1;
	}

	int V;
	// 创建空队列, MaxSize为外部定义的常数 
	dist[S] = 0; // 初始化源点 
	

	while (!myQueue.empty()) {
		V = myQueue.front();
		myQueue.pop();
		for (int W = 1; W <= N + 1; W++) {
			if (Graph[V][W]==1) {
				if (dist[W] == -1) { // 若W->AdjV未被访问过 
					dist[W] = dist[V] + 1; // W->AdjV到S的距离更新 
					path[W] = V; // 将V记录在S到W->AdjV的路径上 
					myQueue.push(W);
				}
			}
		}

	}
	if (dist[N + 1] == -1) cout << 0 << endl;
	else {
		cout << dist[N + 1] << endl;
		int W = path[N + 1];
		while (W != S) {
			myStack.push(W);
			W = path[W];
		}
		while (!myStack.empty()) {
			W = myStack.top();
			myStack.pop();
			cout << Cordlx[W] << " " << Cordly[W] << endl;
		}
	}

}

测试结果:

发布了174 篇原创文章 · 获赞 394 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/tiao_god/article/details/105293393