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
需要记录路径,不妨结构体加个元素记录bfs时的上一个点,这样可以找到路径,另外对于存在多种解的情况,要求输出第一跳最小的情况,那就在找到所有第一跳后先按距离升序排序在进行bfs。代码:
#include <stdio.h> #include <stdlib.h> #include <math.h> struct crocodile { int x,y,l; }c[100],q[100],temp; int n,d,head,tail,flag; int check(int x,int y) { x = abs(x); y = abs(y); return x + d >= 50 || y + d >= 50; } void getpath(int d,int num) { if(d == -1) printf("%d\n",num); else { getpath(q[d].l,num + 1); printf("%d %d\n",q[d].x,q[d].y); } } int cmp(const void *a,const void *b) { struct crocodile aa = *(struct crocodile *)a,bb = *(struct crocodile *)b; return pow(aa.x,2) + pow(aa.y,2) - pow(bb.x,2) - pow(bb.y,2); } int main() { scanf("%d%d",&n,&d); for(int i = 0;i < n;i ++) { scanf("%d%d",&c[i].x,&c[i].y); if(sqrt(pow(c[i].x,2) + pow(c[i].y,2)) - 7.5 <= d) { c[i].l = -1; q[tail ++] = c[i]; i --,n --; } } qsort(q,tail,sizeof(q[0]),cmp); while(head < tail) { temp = q[head ++]; if(check(temp.x,temp.y)) { flag = head - 1; break; } for(int i = 0;i < n;i ++) { if(sqrt(pow(c[i].x - temp.x,2) + pow(c[i].y - temp.y,2)) <= d) { c[i].l = head - 1; q[tail ++] = c[i]; c[i --] = c[-- n]; } } } if(flag) getpath(flag,1); else printf("%d",d + 7.5 >= 50 ? 1 : 0);//一步就可达的情况 或 不可达 return 0; }
原文地址:https://www.cnblogs.com/8023spz/p/12264160.html