本文主要是介绍07-图5 Saving James Bond - Hard Version (30分),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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
#include<bits/stdc++.h>
using namespace std;
struct Node {double x, y;double distant;int c;
};
struct TS {int num;double distant;
};TS HHH[105];
int N, path[105];
double D, G[105][105];
Node Dist[105];
bool visit[105] = {false};void Init(int i, int j);
int BFS(int i);
int Judge(int i);
bool cmp(TS a, TS b) {return a.distant < b.distant;
}int main() {cin >> N >> D;if (D >= 32.5) {printf("1\n");return 0;}int X, Y;int tol = 0;fill(G[0], G[0] + 105*105, 200);for (int i = 0; i < N; i++) {cin >> X >> Y;HHH[i].num = i;Dist[i].x = X;Dist[i].y = Y;Dist[i].distant = sqrt(X * X + Y * Y);HHH[i].distant = Dist[i].distant;Dist[i].c = 0;path[i] = -1;}for (int i = 0; i < N - 1; i++) {for (int j = i + 1; j < N; j++) {Init(i, j);}}sort(HHH, HHH + N, cmp);for (int i = 0; i < N; i++) {if (HHH[i].distant > 7.5 && HHH[i].distant <= D + 7.5 && !visit[HHH[i].num]) {tol = BFS(HHH[i].num);if (tol > 1) break;}}if (tol == 0) printf("0\n");return 0;
}void Init(int i, int j) {int x = Dist[i].x - Dist[j].x;int y = Dist[i].y - Dist[j].y;G[i][j] = G[j][i] = sqrt(x * x + y * y);
}int Judge(int i) {if ((Dist[i].x + D >= 50) || (Dist[i].x - D <= -50) || (Dist[i].y + D >= 50) || (Dist[i].y - D <= -50)) return 1;else return 0;
}int BFS(int i) {visit[i] = true;Dist[i].c = 1;queue<int> Q;Q.push(i);while (!Q.empty()) {int temp = Q.front();Q.pop();if (Judge(temp)) {cout << Dist[temp].c + 1 << endl;stack<int> st;while (temp != -1) {st.push(temp);temp = path[temp];}while (!st.empty()) {int Top = st.top();st.pop();cout << Dist[Top].x << " " << Dist[Top].y << endl;}return 2;}for (int i = 0; i < N; i++) {if (G[temp][i] <= D && !visit[i]) {visit[i] = true;Dist[i].c = Dist[temp].c + 1;path[i] = temp;Q.push(i);}}}return 0;
}
这篇关于07-图5 Saving James Bond - Hard Version (30分)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!