Saving James Bond - Easy Version
Posted 浪漫逆风
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Saving James Bond - Easy Version相关的知识,希望对你有一定的参考价值。
本博客的代码的思想和图片参考:好大学慕课浙江大学陈越老师、何钦铭老师的《数据结构》
Question:
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 whether or not he can escape.
Input Specification:
Each input file contains one test case. Each case starts with a line containing two positive integers NNN (≤100\\le 100≤100), the number of crocodiles, and DDD, the maximum distance that James could jump. Then NNN lines follow, each containing the (x,y)(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, print in a line "Yes" if James can escape, or "No" if not.
Sample Input 1:
14 20
25 -15
-25 28
8 49
29 15
-35 -2
5 28
27 -29
-8 -28
-20 -35
-25 -20
-13 29
-30 15
-35 40
12 12
Sample Output 1:
Yes
Sample Input 2:
4 13
-12 12
12 12
-12 -12
12 -12
Sample Output 2:
No
The code if followed,the algorithm thoughs is in the top of the code,you can read them before you read the program
1 /* 2 * save.c 3 * 4 * Created on: 2017年5月7日 5 * Author: ygh 6 */ 7 #include <stdio.h> 8 #include <stdlib.h> 9 #include <math.h> 10 11 /* 12 * Algorithm thoughts: 13 * we see every crocodile as a point in graph,and we use BFS or DFS to 14 * search the path the James can reach river bank. 15 * In this question,we store graph use a array,not the standard graph,because 16 * in this question, the point in graph is not only crocodies but also the river band 17 * how to show them by a graph. So we give up use traditional graph to show them 18 * and use a array to show them. 19 * But how to judge the adjacent point. we use this point as the central,use the max 20 * jump distance as the radius, that which points in this circle is this 21 * point\'s adjacent point. 22 * 23 * We write the program of BFS and DFS,you can choose one of them of run the program. 24 */ 25 26 #define MAX_LENGTH 100 27 28 typedef int vertex; 29 30 /* 31 * Define a data structure to store a point 32 */ 33 typedef int node1Element; 34 typedef struct node1 { 35 node1Element x; 36 node1Element y; 37 } point[MAX_LENGTH]; 38 39 typedef struct node2 *pGraph; 40 typedef struct node2 { 41 int vertex_num; 42 int maxDistance; 43 point collection; 44 }; 45 46 /* 47 * Build a graph by a list of points 48 */ 49 pGraph buildGraph() { 50 int vertexMum, distance, i; 51 vertex x, y; 52 scanf("%d", &vertexMum); 53 scanf("%d", &distance); 54 pGraph graph = (pGraph) malloc(sizeof(struct node2)); 55 graph->vertex_num = vertexMum; 56 graph->maxDistance = distance; 57 if (graph->maxDistance) { 58 for (i = 0; i < graph->vertex_num; i++) { 59 scanf("%d %d", &x, &y); 60 graph->collection[i].x = x; 61 graph->collection[i].y = y; 62 } 63 } 64 return graph; 65 } 66 67 /*==============================define a queue=====================================================*/ 68 /*define a list to store the element in the queue*/ 69 typedef vertex elementType; 70 typedef struct node3 *pList; 71 typedef struct node3 { 72 elementType element; 73 struct node3 *next; 74 }; 75 76 /*define a queue to point the list*/ 77 typedef struct node4 *pQueue; 78 typedef struct node4 { 79 pList front; /*the front point to point the head of the list*/ 80 pList rear; /*the rear point to point the rear of of the list*/ 81 }; 82 83 /*create a empty list to store the queue element*/ 84 pList createEmptyList() { 85 pList list; 86 list = (pList) malloc(sizeof(struct node3)); 87 list->next = NULL; 88 return list; 89 } 90 /*create a empty queye*/ 91 pQueue createEmptyQueue() { 92 pQueue queue = (pQueue) malloc(sizeof(struct node4)); 93 queue->front = NULL; 94 queue->rear = NULL; 95 return queue; 96 } 97 98 /* 99 Wether the queue is empty 100 @param queue The queue need to adjust 101 @return If the queue is null,return 1 otherwise return 0 102 */ 103 int isQueueEmpty(pQueue queue) { 104 return (queue->front == NULL); 105 } 106 107 /* 108 Add a element to a queue,If the queue is null,we will create a new queue 109 @parama queue The queue we will add elememt to 110 @prama element The element we will add to queue 111 */ 112 void addQueue(pQueue queue, elementType element) { 113 if (isQueueEmpty(queue)) { 114 pList list = createEmptyList(); 115 list->element = element; 116 queue->front = queue->rear = list; 117 } else { 118 pList newNode = (pList) malloc(sizeof(struct node3)); 119 newNode->element = element; 120 newNode->next = queue->rear->next; 121 queue->rear->next = newNode; 122 queue->rear = newNode; 123 } 124 } 125 126 /* 127 delete a element from a queue 128 @param queue The queue will be deleted a element 129 @return The element has been deleted 130 */ 131 elementType deleteEleFromQueue(pQueue queue) { 132 if (isQueueEmpty(queue)) { 133 printf("the queue is empty,don\'t allow to delete elemet from it!"); 134 return -1; 135 } else { 136 pList oldNode = queue->front; 137 elementType element = oldNode->element; 138 if (queue->front == queue->rear) { 139 queue->rear = queue->front = NULL; 140 } else { 141 queue->front = queue->front->next; 142 } 143 free(oldNode); 144 return element; 145 } 146 } 147 148 /* 149 * Calculate the distance from point to safe place 150 * @param x The x-coordinate of the point 151 * @param y THe y-coordinate of the point 152 * @param xLine The x base line of square 153 * @param yLine The y base line of the square 154 */ 155 int calculateDtoSafe(int x, int y, int xLine, int yLine) { 156 int distanceX, distanceY; 157 distanceX = abs(x - xLine); 158 distanceY = abs(y - yLine); 159 return (distanceX > distanceY ? distanceY : distanceX); 160 } 161 162 /** 163 * Calculate two points distance 164 * @return The float distance of twp points 165 */ 166 float calculateTwoPointDistance(int x1, int y1, int x2, int y2) { 167 return (sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))); 168 } 169 170 /* 171 * Judge the point that whether the James Bond can jump into river bank 172 * @param graph A graph to stored all points\' collection 173 * @param point The index of point in the points\' collection 174 * @return 1 will be return if James Bond can jump into river bank, otherwise 175 * return 0 176 */ 177 int isSafe(pGraph graph, vertex point) { 178 int x, y, distance; 179 x = graph->collection[point].x; 180 y = graph->collection[point].y; 181 if (x > 0 && y > 0) { 182 distance = calculateDtoSafe(x, y, 50, 50); 183 } 184 if (x > 0 && y < 0) { 185 distance = calculateDtoSafe(x, y, 50, -50); 186 } 187 188 if (x < 0 && y > 0) { 189 distance = calculateDtoSafe(x, y, -50, 50); 190 } 191 192 if (x < 0 && y < 0) { 193 distance = calculateDtoSafe(x, y, -50, -50); 194 } 195 196 if (distance <= graph->maxDistance) { 197 return 1; 198 } else { 199 return 0; 200 } 201 } 202 203 int isJump(pGraph graph, vertex source, vertex destination) { 204 if (calculateTwoPointDistance(graph->collection[source].x, 205 graph->collection[source].y, graph->collection[destination].x, 206 graph->collection[destination].y) > graph->maxDistance) { 207 return 0; 208 } else { 209 return 1; 210 } 211 } 212 213 /* 214 Breadth first search and find whether having a safe path to reach river bank 215 @param graph The graph stored by the adjacent table 216 @param startPoint The point we start search 217 @param visited A array to tag the elemeent whether has been visited 218 @retuen 219 */ 220 int BFS(pGraph graph, vertex startPoint, int *visited) { 221 visited[startPoint] = 1; 222 /* 223 * The point index in the graph\'s points\' collection 224 */ 225 vertex w, v; 226 int flag = 0; 227 pQueue queue = createEmptyQueue(); 228 addQueue(queue, startPoint); 229 if (isSafe(graph, startPoint)) { 230 flag = 1; 231 return flag; 232 } else { 233 while (!isQueueEmpty(queue)) { 234 w = deleteEleFromQueue(queue); 235 for (v = 0; v < graph->vertex_num; v++) { 236 if (visited[v] == 0 && isJump(graph, v, w)) { 237 if (isSafe(graph, v)) { 238 flag = 1; 239 return flag; 240 } 241 visited[v] = 1; 242 addQueue(queue, v); 243 } 244 } 245 } 246 } 247 return flag; 248 } 249 250 /* 251 Depth first search and find whether having a safe path to reach river bank 252 @param graph The graph stored by the adjacent table 253 @param startPoint The point we start search 254 @param visited A array to tag the elemeent whether has been visited 255 @retuen 256 */ 257 int DFS(pGraph graph, vertex startPoint, int *visited) { 258 int flag = 0; 259 vertex v; 260 visited[startPoint] = 1; 261 if (isSafe(graph, startPoint)) { 262 flag = 1; 263 return flag; 264 } else { 265 for (v = 0; v < graph->vertex_num; v++) { 266 if (visited[v] == 0 && isJump(graph, startPoint, v)) { 267 flag = DFS(graph, v, visited); 268 if (flag == 1) { 269 return flag; 270 } 271 } 272 } 273 } 274 return flag; 275 } 276 277 /* 278 *James first jump action,similar of the isJump method,but sometimes ewe 279 *need to consider the radius if the start points, so we separate the 280 *first jump from followed jumps 281 *@param graph The graph to store points collection and some information 282 *@param startPoint_x The x-coordinate of the start point 283 *@param startPoint_y The x-coordinate of the start point 284 *@param v The index of the point in the graph\'s collection 285 */ 286 int firstJump(pGraph graph, int startPoint_x, int startPoint_y, vertex v) { 287 if (calculateTwoPointDistance(startPoint_x, startPoint_y, 288 graph->collection[v].x, graph->collection[v].y) 289 > graph->maxDistance) { 290 return 0; 291 } else { 292 return 1; 293 } 294 } 295 296 /* 297 * Save James By BFS method 298 *@param graph The graph to store points collection and some information 299 *@param startPoint_x The x-coordinate of the start point 300 *@param startPoint_y The x-coordinate of the start point 301 *@param v The index of the point in the graph\'s collection 302 *@param visited A array to tag which point has been accessed 303 */ 304 int saveJamesBFS(pGraph graph, int startPoint_x, int startPoint_y, int *visited) { 305 vertex v; 306 int flag = 0; 307 for (v = 0; v < graph->vertex_num; v++) { 308 if (visited[v] == 0 309 && firstJump(graph, startPoint_x, startPoint_y, v)) { 310 flag = BFS(graph, v, visited); 311 if (flag == 1) { 312 return flag; 313 } 314 } 315 } 316 return flag; 317 } 318 319 /* 320 * Save James By BFS method 321 *@param graph The graph to store points collection and some information 322 *@param startPoint_x The x-coordinate of the start point 323 *@param startPoint_y The x-coordinate of the start point 324 *@param v The index of the point in the graph\'s collection 325 *@param visited A array to tag which point has been accessed 326 */ 327 int saveJamesDFS(pGraph graph, int startPoint_x, int startPoint_y, int *visited) { 328 vertex v; 329 int flag = 0; 330 for (v = 0; v < graph->vertex_num; v++) { 331 if (visited[v] == 0 332 && firstJump(graph, startPoint_x, startPoint_y, v)) { 333 flag = DFS(graph, v, visited); 334 if (flag == 1) { 335 return flag; 336 } 337 } 338 } 339 return flag; 340 } 341 342 /* 343 * Initialize a visited array that make them all to zero 344 */ 345 void initVisited(pGraph graph, int *visited) { 346 int i; 347 for (i = 0; i < graph->vertex_num; i++) { 348 visited[i] = 0; 349 } 350 } 351 352 /* 353 * Run method 354 */ 355 int main() { 356 pGraph graph = buildGraph(); 357 int visited[graph->vertex_num]; 358 initVisited(graph, visited); 359 int flag = saveJamesBFS(graph, 0, 0, visited); 360 if (flag) { 361 printf("Yes\\n"); 362 } else { 363 printf("No\\n"); 364 } 365 return 0; 366 }
But there is a question of this code by PTA test
If you know the reason,pleae tell me,I thank you very much
以上是关于Saving James Bond - Easy Version的主要内容,如果未能解决你的问题,请参考以下文章
Saving James Bond - Easy Version (MOOC)
06-图2 Saving James Bond - Easy Version (25 分)
06-图2 Saving James Bond - Easy Version(25 分)
05-图2. Saving James Bond - Easy Version (25)