06-图4. 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 1510 -2110 21-40 1030 -5020 4035 100 -10-25 2240 -40-30 30-10 220 1125 2125 1010 1010 35-30 10Sample Output 1:
40 1110 2110 35Sample Input 2:
4 13-12 1212 12-12 -1212 -12Sample Output 2:
0
本题测试点5是从小岛范围内可以直接跳到岸边。
1 #include2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 using namespace std; 10 #define R 7.5 11 struct point 12 { 13 int x,y,last; 14 bool vis; 15 point() 16 { 17 vis=false; 18 } 19 point(int num) 20 { 21 vis=false; 22 last=num; 23 } 24 }; 25 point p[105]; 26 queue q; 27 double getdis(int x1,int y1,int x2,int y2) 28 { 29 int x=x1-x2; 30 int y=y1-y2; 31 return sqrt(x*x+y*y); 32 } 33 int abs(int a) 34 { 35 return a>0?a:-a; 36 } 37 int main() 38 { 39 //freopen("D:\\INPUT.txt","r",stdin); 40 int n,J; 41 scanf("%d %d",&n,&J); 42 int i,level=1,last=-1,tail; 43 for(i=0; i =50){ //小岛内直接跳到岸边 49 printf("1\n"); 50 return 0; 51 } 52 for(i=0; i =getdis(0,0,p[i].x,p[i].y)) 59 { 60 q.push(i); 61 p[i].vis=true; 62 last=i; 63 if(abs(p[i].x)+J>=50||abs(p[i].y)+J>=50) 64 { 65 printf("2\n"); 66 printf("%d %d\n",p[i].x,p[i].y); 67 return 0; 68 } 69 } 70 } 71 if(last==-1){ //一开始就没有可以跳的点 72 printf("0\n"); 73 return 0; 74 } 75 int cur,firstj=J+1,fitp,count=0; 76 while(!q.empty()) 77 { 78 cur=q.front(); 79 q.pop(); 80 for(i=0; i =getdis(p[cur].x,p[cur].y,p[i].x,p[i].y)) 83 { //没有入队并且符合要求 84 p[i].vis=true; 85 q.push(i);//入队 86 p[i].last=cur; 87 tail=i; 88 if(abs(p[i].x)+J>=50||abs(p[i].y)+J>=50) 89 { 90 int now=i; 91 while(p[now].last!=now){ 92 now=p[now].last; 93 } 94 if(getdis(0,0,p[now].x,p[now].y)-R