码迷,mamicode.com
首页 > 其他好文 > 详细

Saving James Bond - Hard Version(PAT)

时间:2018-04-12 15:36:41      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:was   while   sla   location   ecif   sid   lin   minimum   click   

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 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<stdio.h>
  2 #include<math.h>
  3 #include<stdlib.h>
  4 #define MAX 101
  5 #define IsLand 15.0/2;
  6 int visited[MAX];
  7 int Path[MAX];
  8 int N;
  9 double R;
 10  
 11 typedef struct Position{
 12     double x;
 13     double y;
 14     int flag;
 15 }Position; 
 16 
 17 typedef struct Queue* ListQ;
 18 typedef struct Queue
 19 {
 20     int Data[MAX];
 21     int rear;
 22     int front;
 23 }Queue;
 24 
 25 typedef struct Stack* StackQ;
 26 typedef struct Stack
 27 {
 28     int Data[MAX];
 29     int Top;
 30 }Stack;
 31 
 32 Position G[101];
 33 ListQ P=NULL;
 34 StackQ Head=NULL;
 35 
 36 ListQ Creat()
 37 {
 38     ListQ P=(ListQ)malloc(sizeof(Queue));
 39     P->rear=P->front=0;
 40     return P;
 41 }
 42 
 43 void AddQ(ListQ P,int X)
 44 {
 45     if((P->rear+1)%MAX==P->front)
 46     {
 47         printf("队列满\n");
 48         return;
 49     }
 50     P->rear=(P->rear+1)%MAX;
 51     P->Data[P->rear]=X;
 52     return;
 53 }
 54 
 55 int IsEmpty(ListQ P)
 56 {
 57     return P->rear==P->front;
 58 }
 59 
 60 int DeleteQ(ListQ P)
 61 {
 62     if(P->rear==P->front)
 63     {
 64         printf("队列空\n");
 65         return -1;
 66     }
 67     P->front=(P->front+1)%MAX;
 68     return P->Data[P->front];
 69 }
 70 
 71 StackQ CreatS()
 72 {
 73     StackQ P=(StackQ)malloc(sizeof(Stack));
 74     P->Top=-1;
 75     return P;
 76 }
 77 
 78 void Push(StackQ P,int X)
 79 {
 80     if (P->Top==MAX-1)
 81     {
 82         printf("满\n");
 83         return;
 84     }
 85     else
 86     {
 87         P->Data[++P->Top]=X;
 88         return;
 89     }
 90 }
 91 
 92 int IsEmptyS(StackQ P)
 93 {
 94     return P->Top==-1;
 95 }
 96 
 97 int Pop(StackQ P)
 98 {
 99     if (P->Top==-1)
100     {
101         printf("空\n");
102         return -1;
103     }
104     else
105         return (P->Data[P->Top--]);
106 }
107 
108 
109 int JumpOut(int v)   
110 {
111     int a,b,c;
112     if(G[v].x<0)
113         a=G[v].x+50;
114     else
115         a=50-G[v].x;
116     
117     if(G[v].y<0)
118         b=G[v].y+50;
119     else
120         b=50-G[v].y;
121     if(a>b)
122         c=b;
123     else
124         c=a;
125     if(c>R)
126         return 0;
127     else
128         return 1;
129 }
130 
131 int FirstJump(int v)   
132 {
133     return sqrt((G[v].x*G[v].x)+(G[v].y*G[v].y))<=R+IsLand;   //加上小岛本身的距离
134  } 
135 
136 int MinFirstJump(int v)   
137 {
138     return (G[v].x*G[v].x)+(G[v].y*G[v].y);   //加上小岛本身的距离
139  } 
140  
141 int Jump(int i,int v)        
142 {
143     return sqrt((G[v].x-G[i].x)*(G[v].x-G[i].x)+(G[v].y-G[i].y)*(G[v].y-G[i].y))
144     <=R;
145 }
146 
147 void BFS()
148 {
149     int v;
150     while(!IsEmpty(P))
151     {
152         int i;
153         int v=DeleteQ(P);
154         if(JumpOut(v)) 
155         {
156             G[v].flag=1;
157         }
158         for(i=0;i<N;i++)
159         {
160             if(visited[i]==-1 && Jump(i,v))
161             {
162                 visited[i]=visited[v]+1;
163                 Path[i]=v;
164                 AddQ(P,i);
165             }    
166         }
167     }
168     return;
169 } 
170 
171 void Sava007()
172 {
173     int v;
174     for(v=0;v<N;v++)    
175     {    
176         if(visited[v]==-1 && FirstJump(v))
177         {
178              visited[v]=1;
179             Path[v]=N;
180              AddQ(P,v);         
181         }
182     }
183     BFS();
184     return;
185 }
186 
187 void Out()
188 {
189     int v;
190     int sum=0,k;
191      int min=1000,minSum=0;
192     for (v = 0; v < N; v++)
193     {
194         if(G[v].flag==1)
195         {
196             sum++;
197             k=v;    
198         }
199     }
200     if (sum==1)
201     {
202         printf("%d\n",visited[k]+1);
203         for (v=k;v<N;v=Path[v])
204             Push(Head,v);
205         while(!IsEmptyS(Head))
206         {
207             v=Pop(Head);
208             printf("%0.0f %0.0f\n",G[v].x,G[v].y);
209         }
210     }
211     else if (sum>1)
212     {    
213         for (v = 0; v < N; v++)
214         {
215             if( G[v].flag==1 && visited[v]<=min)
216             {
217                 min=visited[v];
218                 k=v;
219             }
220         }
221         for (v = 0; v < N; v++)
222         {
223             if( G[v].flag==1 && visited[v]==min)
224                 minSum++;
225         }
226         if (minSum==1)
227         {
228             printf("%d\n",visited[k]+1);
229             for (v=k;v<N;v=Path[v])
230                 Push(Head,v);
231             while(!IsEmptyS(Head))
232             {
233                 v=Pop(Head);
234                 printf("%0.0f %0.0f\n",G[v].x,G[v].y);
235             }
236         }
237         else
238         {
239             
240             int i=0,j=0,m=0;
241             int MinAnswer[MAX];
242             int Answer[MAX];
243             for (v=k;v<N;v=Path[v])
244                 Push(Head,v);
245             while(!IsEmptyS(Head))
246                 MinAnswer[i++]=Pop(Head);
247              for (v = 0; v < N; v++)
248              {
249                  m=0;
250                  if( G[v].flag==1 && visited[v]==min)
251                  {
252                      for (j=v;j<N;j=Path[j])
253                         Push(Head,j);
254                      while(!IsEmptyS(Head))
255                         Answer[m++]=Pop(Head);
256                      if(MinFirstJump(Answer[0])<MinFirstJump(MinAnswer[0]))
257                      {
258                          for(j=0;j<i;j++)
259                          {
260                             MinAnswer[j]=Answer[j];
261                          }
262                     }
263                  }
264              }
265              printf("%d\n",visited[k]+1);
266              for(j=0;j<i;j++)
267                  printf("%0.0f %0.0f\n",G[MinAnswer[j]].x,G[MinAnswer[j]].y);
268         }
269     }
270     else
271         printf("0\n");
272 }
273 
274 int main()
275 {
276     int i;
277     P=Creat();
278     Head=CreatS();
279     scanf("%d %lf",&N,&R);    //注意:跳的距离不一定是整数
280     for(i=0;i<MAX;i++)
281     {
282         visited[i]=-1;
283         Path[i]=-1;
284     }
285     for(i=0;i<N;i++)
286     {
287         scanf("%lf %lf",&G[i].x,&G[i].y); //注意:位置不一定是整数
288         G[i].flag=0;
289         
290     }
291     if (R>=43)
292         printf("1\n");
293     else
294     {
295         Sava007();
296         Out();
297     }
298     return 0;
299 } 
View Code

 

Saving James Bond - Hard Version(PAT)

标签:was   while   sla   location   ecif   sid   lin   minimum   click   

原文地址:https://www.cnblogs.com/tzfs/p/8807486.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!