pat06-图4. Saving James Bond - Hard Version (30)
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 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 10Sample Output 1:
4 0 11 10 21 10 35Sample Input 2:
4 13 -12 12 12 12 -12 -12 12 -12Sample Output 2:
0
本题测试点5是从小岛范围内可以直接跳到岸边。
1 #include<cstdio> 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<queue> 6 #include<stack> 7 #include<cmath> 8 #include<string> 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<int> 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<n; i++) 44 { 45 scanf("%d %d",&p[i].x,&p[i].y); 46 p[i].last=i; 47 } 48 if(R+J>=50){//小岛内直接跳到岸边 49 printf("1\n"); 50 return 0; 51 } 52 for(i=0; i<n; i++) 53 { 54 if(getdis(0,0,p[i].x,p[i].y)<=R){//无效点 55 p[i].vis=true; 56 continue; 57 } 58 if(R+J>=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<n; i++) 81 { 82 if(!p[i].vis&&J>=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<firstj){ 95 firstj=getdis(0,0,p[now].x,p[now].y)-R; 96 fitp=i; 97 count=level+1; 98 } 99 } 100 } 101 } 102 if(cur==last){ 103 level++;//向下一层进发 104 last=tail; 105 } 106 if(level==count){ 107 break; 108 } 109 } 110 stack<int> s; 111 if(count) 112 { 113 count++; 114 while(p[fitp].last!=fitp) 115 { 116 s.push(fitp); 117 fitp=p[fitp].last; 118 } 119 s.push(fitp); 120 } 121 printf("%d\n",count); 122 while(!s.empty()) 123 { 124 printf("%d %d\n",p[s.top()].x,p[s.top()].y); 125 s.pop(); 126 } 127 return 0; 128 }