07-图5 Saving James Bond - Hard Version(30 分)

Posted 思而不学

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了07-图5 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


我的答案(最大N没过)
  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <unistd.h>
  4 #include <math.h>
  5 
  6 #define QMAXSIZE 10000
  7 
  8 struct Crocodile {
  9     int x;
 10     int y;
 11     int Visited;
 12     int Path;
 13 };
 14 typedef struct Crocodile *Point;
 15 
 16 //队列部分
 17 struct QNode {
 18     int Data[QMAXSIZE];
 19     int rear;
 20     int front;
 21 };
 22 typedef struct QNode *Queue;
 23 
 24 int IsEmpty(Queue Q)
 25 {
 26     return (Q->rear == Q->front);
 27 }
 28 
 29 void AddQ(Queue PtrQ, int item)
 30 {
 31     if((PtrQ->rear+1)%QMAXSIZE == PtrQ->front) {
 32         printf("Queue full");
 33         return;
 34     }
 35     PtrQ->rear = (PtrQ->rear+1)%QMAXSIZE;
 36     PtrQ->Data[PtrQ->rear] = item;
 37 }
 38 
 39 int DeleteQ(Queue PtrQ)
 40 {
 41     if(PtrQ->front == PtrQ->rear) {
 42         printf("Queue empty");
 43         return -1;
 44     } else {
 45         PtrQ->front = (PtrQ->front+1)%QMAXSIZE;
 46         return PtrQ->Data[PtrQ->front];
 47     }
 48 }
 49 
 50 void PrintQ(Queue PtrQ)
 51 {
 52     int i;
 53     printf("[Queue]: ");
 54     for(i=(PtrQ->front+1)%QMAXSIZE;i!=(PtrQ->rear+1)%QMAXSIZE
 55             ;i=(i+1)%QMAXSIZE)
 56         printf("%d ", PtrQ->Data[i]);
 57     printf("\n");
 58 }//end Queue
 59 
 60 void ReadPoint(Point P, int N)
 61 {
 62     int i;
 63     P[0].x = 0;
 64     P[0].y = 0;
 65     P[0].Visited = 0;
 66     P[0].Path = -1;
 67     for(i=1;i<N;i++) {      //N=N+1
 68         scanf("%d %d\n", &P[i].x, &P[i].y);
 69         P[i].Visited = 0;
 70     }
 71 }
 72 
 73 void PrintfPoint(Point P, int N)
 74 {
 75     int i;
 76     for(i=0;i<N;i++) {      //N=N+1
 77         printf("P[%d] X:%d Y:%d\n", i, P[i].x, P[i].y);
 78     }
 79     printf("----------------------------\n");
 80 }
 81 
 82 void PrintPath(Point P, int stand)
 83 {
 84     int Path[100], i;
 85     // printf("[PrintPath] stand=%d\n", stand);
 86     // if(P[stand].Path != -1) {
 87     //     PrintPath(P, P[stand].Path);
 88     //     printf("%d %d\n", P[stand].x, P[stand].y);
 89     // }   
 90     if(stand==0) printf("1\n");
 91     else {
 92         for(i=0;P[stand].Path != -1;i++) {
 93             Path[i] = stand;
 94             stand = P[stand].Path;
 95         }   
 96         // printf("[PrintPaht] i=%d\n", i);     
 97         printf("%d\n", i+1);
 98         for(i--;i>=0;i--) {
 99             printf("%d %d\n", P[Path[i]].x, P[Path[i]].y);
100         }
101     }
102 }
103 
104 double PointDistance(Point P1, Point P2)
105 {
106     return sqrt(pow((P1->x - P2->x), 2) + pow((P1->y - P2->y), 2));
107 }
108 
109 double FindMinPath(Point P, int stand)
110 {
111     while(P[stand].Path != 0) {
112         stand = P[stand].Path;
113     }
114     return PointDistance(&P[0], &P[stand]);
115 }
116 
117 int IsUp(Point P, int stand, double D, int island)
118 {
119     int xlen = 50-abs(P[stand].x);
120     int ylen = 50-abs(P[stand].y);
121     if(island == 1 && (xlen<=(D+7.5) || ylen<=(D+7.5)))
122         return 1;
123     else if(stand!=1 && (xlen<=D || ylen <=D))
124         return 1;
125     return 0;
126 }
127 
128 int IsUseless(Point P, int stand)
129 {
130     if(abs(P[stand].x) <= 7.5 && abs(P[stand].y) <= 7.5 )
131         return 1;
132     else if(abs(P[stand].x == 50 && abs(P[stand].y == 50)))
133         return 1;
134     else 
135         return 0;
136 }
137 
138 void Visit(Point P, int stand)
139 {
140     printf("[Point] P.x:%d y:%d\n", P[stand].x, P[stand].y);
141 }
142 
143 int BFS(Point P, int N, double D, int stand)
144 {
145     Queue Q;
146     int S, i, endP=-1;
147     double dist, island = 0, minDist=100;
148 
149     Q = (Queue)malloc(sizeof(struct QNode)*N);
150     // Visit(P, stand);                                //访问P[0]
151     P[stand].Visited = 1;
152     AddQ(Q, stand);
153 
154     while(!IsEmpty(Q)) {
155         // PrintQ(Q);
156         S = DeleteQ(Q);                             //提取队列
157         if(S == 0)                                  //是否在岛上
158             island = 1;
159         else        
160             island = 0;
161         if(IsUp(P, S, D, island)) {
162             endP = S;
163             break;
164         }
165 
166         for(i=1;i<N;i++) {
167             if(IsUseless(P, i)) continue;
168             dist = PointDistance(&P[S], &P[i]);
169             if(!P[i].Visited && dist<=(D+(double)island*7.5)) {     //未被访问且能跳到
170                 // Visit(P, i);
171                 P[i].Path = S;
172                 if(IsUp(P, i, D, island)) {   
173                     // printf("[Ok] Here Point can go up\n");
174                     // printf("[Path] \n");
175                     // PrintPath(P, i);
176                     // printf("\n");
177                     double temp;
178                     temp = FindMinPath(P, i);
179                     if(minDist > temp) {
180                         minDist = temp;
181                         endP = i;
182                     }
183                 }
184                 P[i].Visited = 1;
185                 AddQ(Q, i);
186                 // dist = D+1;
187             }
188         }
189     }
190     return endP;
191 }
192 
193 int main()
194 {
195     int N, endP;
196     double D;
197     Point P;
198     scanf("%d %lf", &N, &D);
199     N++;        //从1开始
200     P = (Point)malloc(sizeof(struct Crocodile)*N);
201     ReadPoint(P, N);
202     // PrintfPoint(P, N);
203 
204     endP = BFS(P, N, D, 0);
205     // printf("minP:%d\n", endP);
206     if(endP == -1)
207         printf("0\n");
208     else {
209         PrintPath(P, endP);
210     }
211     return 0;
212 }

 

以上是关于07-图5 Saving James Bond - Hard Version(30 分)的主要内容,如果未能解决你的问题,请参考以下文章

07-图5 Saving James Bond - Hard Version(30 分)

PTA 07-图5 Saving James Bond - Hard Version (30分)

Saving James Bond - Easy Version 原创 2017年11月23日 13:07:33

06-图2 Saving James Bond - Easy Version (25 分)

06-图2 Saving James Bond - Easy Version(25 分)

05-图2. Saving James Bond - Easy Version (25)