zoukankan      html  css  js  c++  java
  • 06-图2 Saving James Bond

    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 (≤), the number of crocodiles, and D, the maximum distance that James could jump. Then N lines follow, each containing the ( 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

    我的答案,岛居然有15的直径T.T
      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <unistd.h>
      4 #include <math.h>
      5 
      6 struct Crocodile {
      7     int x;
      8     int y;
      9     int Visited;
     10 };
     11 typedef struct Crocodile *Point;
     12 
     13 int ReadPoint(Point P, int N);
     14 void PrintPoint(Point P, int N);
     15 double PointDistance(Point P1, Point P2);
     16 int DFS(Point P, int N, double D, int stand);
     17 int IsUp(Point P, int stand, double D);
     18 
     19 int ReadPoint(Point P, int N)
     20 {
     21     int i;
     22     P[0].x = 0;
     23     P[0].y = 0;
     24     P[0].Visited = 0;
     25     for(i=1;i<N;i++) {
     26         scanf("%d %d
    ", &P[i].x, &P[i].y);
     27         P[i].Visited = 0;
     28     }
     29     return 0;
     30 }
     31 
     32 void PrintPoint(Point P, int N)
     33 {
     34     int i;
     35     for(i=0;i<N;i++) {
     36         printf("P[%d] X:%d Y:%d
    ", i, P[i].x, P[i].y);
     37     }
     38     printf("----------------------------
    ");
     39 }
     40 
     41 double PointDistance(Point P1, Point P2)
     42 {
     43     return sqrt(pow((P1->x - P2->x), 2) + pow((P1->y - P2->y), 2));
     44 }
     45 
     46 int IsUp(Point P, int stand, double D)
     47 {
     48     int xlen = 50-abs(P[stand].x);
     49     int ylen = 50-abs(P[stand].y);
     50     if(stand == 1 && (xlen<=(D+7.5)
     51         || ylen<=(D+7.5))) {
     52         return 1;
     53     } else if(stand!=1 && (xlen<=D || ylen<=D)) {
     54         return 1;
     55     }
     56     return 0;           //Not
     57 }
     58 
     59 int DFS(Point P, int N, double D, int stand)
     60 {
     61     int i, ret=0, isup, island;
     62     // printf("p[%d] X:%d Y:%d ", stand, P[stand].x, P[stand].y);
     63     P[stand].Visited = 1;
     64     if(stand == 0) island = 1;
     65     isup = IsUp(P, stand, D);
     66     if(isup) {
     67         // printf("stand %d
    ", stand);
     68         return 1;
     69     }
     70     for(i=1;i<N;i++) {
     71         if(!P[i].Visited && (PointDistance(&P[i], &P[stand]) <= (D+island*7.5))) {
     72             // printf("D:%lf
    ", PointDistance(&P[i], &P[stand]));
     73             ret = DFS(P, N, D, i);
     74             if(ret) {
     75                 return ret;
     76             }
     77         }
     78     }
     79     return ret;
     80 }
     81 
     82 int main()
     83 {
     84     int N;
     85     double D;
     86     Point P, S;
     87 
     88     S = (Point)malloc(sizeof(struct Crocodile));
     89     S->x = 0;
     90     S->y = 0;
     91 
     92     scanf("%d %lf
    ", &N, &D);
     93     N++;                                            //N = N + 1;
     94     P = (Point)malloc(sizeof(struct Crocodile)*N);
     95     ReadPoint(P, N);
     96     // PrintPoint(P, N);
     97     if(DFS(P, N, D, 0))
     98         printf("Yes
    ");
     99     else
    100         printf("No
    ");
    101 
    102     return 0;
    103 }
    无欲速,无见小利。欲速,则不达;见小利,则大事不成。
  • 相关阅读:
    挖矿病毒入侵-分析总结
    Linux查看包依赖关系的神器-repoquery分享
    Elasticsearch 字段为空(null)记录查询
    Python 导数 Elasticsearch 元数据到CSV
    基于docker快速构建MySQL主从复制环境
    Redis环境简单部署(集群/双机)
    FTP 脚本 to Shell脚本&bat脚本&python脚本
    专用服务器模式&共享服务器模式
    CentOS 7安装部署ELK 6.2.4-SUCCESS
    zabbix 数据库迁移变更
  • 原文地址:https://www.cnblogs.com/ch122633/p/8970046.html
Copyright © 2011-2022 走看看