06-图2 Saving James Bond - Easy Version

课件中有一个讨论:有没有必要用到邻接矩阵或邻接表来存储图?让我在迷茫中想到用一个一维数组来解决问题或许不错,我就这样做了。有一个小点,为了不出现浮点数,我在计算时把中心小岛的半径放大了10倍

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <stdbool.h>
 4 
 5 const int L = 50, R = 75;
 6 
 7 bool DFS(int * a, int i, int N, int D);
 8 bool firstJump(int * a, int i, int D);
 9 bool isSafe(int * a, int i, int D);
10 bool jump(int * a, int i, int j, int D);
11 
12 int main()
13 {
14 //    freopen("in.txt", "r", stdin);
15     int i, N, D;
16     scanf("%d%d", &N, &D);
17     int a[3 * N];
18     for(i = 0; i < N; i++)
19     {
20         scanf("%d%d", &a[3 * i], &a[3 * i + 1]);
21         a[3 * i + 2] = 0;
22     }
23     
24     bool answer = false;
25     for(i = 0; i < N; i++)
26     {
27         if(a[3 * i + 2] == 0 && firstJump(a, i, D))
28         {
29             answer = DFS(a, i, N, D);
30             if(answer)
31                 break;
32         }
33     }
34     
35     if(answer)
36         printf("Yes\n");
37     else
38         printf("No\n");
39 //    fclose(stdin);
40     return 0;
41 }
42 
43 bool DFS(int * a, int i, int N, int D)
44 {
45     bool answer;
46     int j;
47     
48     answer = false;
49     a[3 * i + 2] = 1;
50     if(isSafe(a, i, D))
51         answer = true;
52     else
53     {
54         for(j = 0; j < N; j++)
55         {
56             if(a[3 * j + 2] == 0 && jump(a, i, j, D))
57             {
58                 answer = DFS(a, j, N, D);
59                 if(answer)
60                     break;
61             }
62         }
63     }
64     
65     return answer;
66 }
67 
68 bool firstJump(int * a, int i, int D)
69 {
70     if(100 * (a[3 * i] * a[3 * i] + a[3 * i + 1] * a[3 * i + 1]) > (10 * D + R) * (10 * D + R))
71         return false;
72     else
73         return true;
74 }
75 
76 bool isSafe(int * a, int i, int D)
77 {
78     if(abs(a[3 * i]) < L - D && abs(a[3 * i + 1]) < L - D)
79         return false;
80     else
81         return true;
82 }
83 
84 bool jump(int * a, int i, int j, int D)
85 {
86     int x, y;
87     x = a[3 * i] - a[3 * j];
88     x = x > 0 ? x : -x;
89     y = a[3 * i + 1] - a[3 * j + 1];
90     y = y > 0 ? y : -y;
91     
92     if(x * x + y * y > D * D)
93         return false;
94     else
95         return true;
96 }

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 whether or not he can escape.

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, print in a line "Yes" if James can escape, or "No" if not.

Sample Input 1:

14 20
25 -15
-25 28
8 49
29 15
-35 -2
5 28
27 -29
-8 -28
-20 -35
-25 -20
-13 29
-30 15
-35 40
12 12

Sample Output 1:

Yes

Sample Input 2:

4 13
-12 12
12 12
-12 -12
12 -12

Sample Output 2:

No
posted @ 2015-04-19 20:37  自由的青  阅读(460)  评论(0编辑  收藏  举报