06-Figure 2 Saving James Bond, 06-bond

Source: Internet
Author: User

06-Figure 2 Saving James Bond, 06-bond

There is a discussion in the courseware: Is it necessary to use the adjacent matrix or the adjacent table to store graphs? It may be good for me to think about using a one-dimensional array to solve the problem. I just did this. There is a small point. In order not to show floating point numbers, I increased the radius of the central island by 10 times during calculation.

 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     else38         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     else53     {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     else73         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     else81         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     else95         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 got Med 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 cocould 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 cocould 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 coshould 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 2025 -15-25 288 4929 15-35 -25 2827 -29-8 -28-20 -35-25 -20-13 29-30 15-35 4012 12

Sample Output 1:

Yes

Sample Input 2:

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

Sample Output 2:

No

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.