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 NN (\le 100≤100), the number of crocodiles, andDD, the maximum distance that James could jump. Then NN lines follow, each containing the (x, y)(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)(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. 用bfs求最短路
2. path[]記錄路徑 壓入堆棧 逆序輸出即為踩過的鱷魚~
3. 題目要求 如果有多條最短路徑相同~ 那么輸出第一跳最小的那條路徑~~ (在bfs中將第一跳能跳的所有節點按距離從小到大一次入隊即可)。
4. step記錄總共跳了幾次~ 這里用last記錄bfs中當前層的最后一個元素 tail記錄bfs中下一層的最后一個元素 每次last等於隊列中彈出的元素 說明即將進入下一層
將step+1.
*/
#include "iostream" #include "math.h" #include "queue" #include "stack" #include "algorithm" using namespace std; int n, m; #define MINLEN 42.5 struct Pointer { int x; int y; }point[101]; bool answer = false; /* 記錄007能否安全逃生~~ */ bool visited[101] = { false }; /* 判斷當前點是否被訪問過 */ int path[101] = {-1}; /* 記錄跳躍過程中踩過的鱷魚 */ bool isSave(int x) { /* 判斷從當前點能否跳到岸上 */ if ((point[x].x - m <= -50) || (point[x].x + m >= 50) || (point[x].y - m <= -50) || (point[x].y + m >= 50)) return true; return false; } bool jump(int x, int y) { /* 判斷2個點距離是否在跳躍能力內 */ int p1 = pow(point[x].x - point[y].x, 2); int p2 = pow(point[x].y - point[y].y, 2); int r = m * m; if (p1 + p2 <= r) return true; return false; } int firstJump(int x) { /* 當007處於孤島時 第一次可以選擇跳的鱷魚 因為第一次判斷能否跳躍的計算方法與后面dfs不相同 所以要單獨寫 */ int p1 = pow(point[x].x, 2); int p2 = pow(point[x].y, 2); int r = (m + 7.5) * (m + 7.5); if (p1 + p2 <= r) { return p1+p2; } return 0; } bool cmp(int a,int b) { return firstJump(a) < firstJump(b); } void bfs() { /* 用bfs來判斷最少要踩幾個小鱷魚才能上岸 */ int b[101]; queue<int>q; /* 將第一步能踩到的鱷魚按距離從小到大的順序進隊列~ 因為輸出結果要保證在踩的鱷魚數量相等的情況下 輸出第一步距離最短的~~*/ for (int i = 0; i < n; i++) { b[i] = i; } sort(b, b + n, cmp); /* 按照第一步的距離排序~~~ */ int last; for (int i = 0; i < n; i++) { if (firstJump(b[i])) { /* 能跳上去! */ q.push(b[i]); visited[b[i]] = true; /* 指向當前層數最后一個數~ */ last = b[i]; } } int step = 2; /* 記錄最少要跳躍的次數 */ int tail; while (!q.empty()) { int p = q.front(); q.pop(); if (isSave(p)) { int k = 1; stack<int> s; cout << step << endl; while (k < step) { //cout << point[p].x << " " << point[p].y << endl; s.push(p); p = path[p]; k++; } while (!s.empty()) { p = s.top(); s.pop(); cout << point[p].x << " " << point[p].y << endl; } return; } for (int i = 0; i < n; i++) { if (!visited[i] && jump(p, i)) { /* 沒踩過並且能跳到 */ q.push(i); path[i] = p; /* 記得當前進隊節點的父節點~ */ visited[i] = true; tail = i; /* 指向下一層的最后一個元素 */ } } if (last == p) { /* 即將進入下一層~ */ step += 1; last = tail; } } if (q.empty()) { /* 如果隊列為空 說明沒跳出去啊~ */ cout << "0" << endl; } } int main() { cin >> n >> m; for (int i = 0; i < n; i++) { cin >> point[i].x >> point[i].y; } if (m >= MINLEN) { /* 可以直接從孤島上提到岸上 直接輸出 */ cout << "1" << endl; return 0; } bfs(); return 0; }