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 (≤), the number of crocodiles, and D, the maximum distance that James could jump. Then N lines follow, each containing the ( 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 ( 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 #include <cstdio> 2 #include <cmath> 3 #include <algorithm> 4 #include <queue> 5 #include <cstring> 6 #include <stack> 7 using namespace std; 8 9 const int diameter = 15; 10 const int maxn = 102; 11 int D, Ncro; 12 13 struct coordinates { 14 int x, y; 15 } cro_pos[maxn]; 16 17 18 struct JNode { 19 int final; 20 int length, start; 21 22 } ans_set[maxn]; 23 24 25 coordinates Origin; 26 27 void init(int Ncro); 28 bool jumpJudge(coordinates a, coordinates b); 29 bool arrive(int i); 30 bool firstJudge(coordinates a, coordinates b); 31 32 bool reachBank(int vertex); 33 double dist(coordinates a, coordinates b); 34 35 36 37 38 void weighted(int, int); 39 bool cmp(JNode a, JNode b); 40 bool findShortest(int ); 41 42 int Distance[maxn]; 43 int Parent[maxn]; 44 int NearBank[maxn]; 45 46 47 48 int main() { 49 scanf("%d %d", &Ncro, &D); 50 init(Ncro); 51 stack<coordinates> st; 52 Distance[0] = 0; 53 54 if(reachBank(0) ) {NearBank[0] = true;} 55 56 for(int V=1; V<=Ncro; V++) { 57 58 if ( firstJudge(cro_pos[V], Origin) ) { 59 Distance[V] = 1; 60 Parent[V] = 0; 61 weighted(V, Ncro); 62 } 63 } 64 65 if( !findShortest(Ncro) ) printf("0\n"); 66 else { 67 printf("%d\n", ans_set[0].length + 1); 68 69 for(int i=ans_set[0].final; i; i = Parent[i] ) { 70 st.push( cro_pos[i] ); 71 } 72 while(!st.empty()) { 73 coordinates tmp = st.top(); 74 st.pop(); 75 76 printf("%d %d\n", tmp.x, tmp.y); 77 78 79 } 80 81 82 } 83 84 } 85 86 bool findShortest(int Ncro) { 87 int num = 0, j; 88 bool flag = false; 89 90 for(int i=0; i<=Ncro; i++) { 91 if(NearBank[i]) { 92 flag = true; 93 ans_set[num].final = i; 94 ans_set[num].length = Distance[i]; 95 96 for(j=i; Parent[j] > 0; j = Parent[j]); 97 ans_set[num].start = j; 98 99 num++; 100 } 101 } 102 103 sort(ans_set, ans_set+num, cmp); 104 105 106 return flag; 107 108 } 109 110 bool cmp(JNode a, JNode b) { 111 if (a.length != b.length) { 112 return a.length < b.length; 113 } 114 else return ( dist(cro_pos[a.start], Origin) < dist(cro_pos[b.start], Origin) ); 115 } 116 117 118 void weighted(int S, int Ncro) { 119 //reset struct ans[s] 120 queue<int> Q; 121 int W; 122 123 Q.push(S); 124 125 while(!Q.empty()) { 126 W = Q.front(); 127 Q.pop(); 128 129 if(reachBank(W) ) {NearBank[W] = true; continue;} 130 131 for(int i=1; i<=Ncro; i++ ) { 132 133 if( jumpJudge( cro_pos[W], cro_pos[i] ) ) 134 if( Distance[i] == -1 ) { 135 Distance[i] = Distance[W] + 1; 136 Parent[i] = W; 137 Q.push(i); 138 } 139 140 141 } 142 } 143 144 145 } 146 147 bool reachBank(int vertex) { 148 int ans = 0; 149 150 if (fabs(cro_pos[vertex].x - 50) <= D) { 151 ans = 1; 152 } 153 154 if (fabs(cro_pos[vertex].x + 50) <= D) { 155 ans = 1; 156 } 157 158 if (fabs(cro_pos[vertex].y + 50) <= D) { 159 ans = 1; 160 } 161 162 if (fabs(cro_pos[vertex].y - 50) <= D) { 163 ans = 1; 164 } 165 166 return ans; 167 } 168 169 170 171 172 void init(int Ncro) { 173 for (int i=1; i<=Ncro; i++) { 174 scanf("%d%d", &cro_pos[i].x, &cro_pos[i].y); 175 } 176 177 Origin.x = 0; 178 Origin.y = 0; 179 memset( Distance, -1, sizeof(Distance) ); 180 memset( Parent, -1, sizeof(Parent) ); 181 } 182 183 bool jumpJudge(coordinates a, coordinates b) { 184 if ( dist(a, b) <= D) { 185 return true; 186 } 187 else return false; 188 } 189 190 191 bool firstJudge(coordinates a, coordinates ori) { 192 193 194 if ( dist(a, ori) <= D + diameter/2 and dist(a, ori) > diameter/2) { 195 196 return true; 197 } 198 return false; 199 } 200 201 double dist(coordinates a, coordinates b) { 202 return sqrt( pow(a.x - b.x, 2) + pow(a.y - b.y, 2) ) ; 203 }

浙公网安备 33010602011771号