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

    深度优先搜索

    这题比较简单,用了数组。在007跳跃范围内,找到鳄鱼,依次直到岸边 return yes。否则No

    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, andD, the maximum distance that James could jump. Then NN 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
     1 #include <iostream>
     2 #include <cstdio>
     3 using namespace std;
     4 #define MaxSize 100
     5 
     6 struct crocodiles
     7 {
     8     float x;
     9     float y;
    10 };
    11 struct crocodiles cro[MaxSize];
    12 
    13 int D;    //007可以跳的距离 
    14 int N;    //鳄鱼数量 
    15 bool Visited[MaxSize] = {false};
    16 
    17 //距离小于 007 可跳距离 返回true  
    18 bool Jump(int x1, int y1, int x2, int y2)
    19 {
    20     if( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) <= D*D)//两点距离小于可跳距离 
    21         return true;
    22     return false; 
    23 }
    24 
    25 //如果007跳到岸边 则返回true 
    26 bool isSafe(int v)
    27 {
    28     if( (cro[v].x + D >= 50) || (cro[v].y + D >= 50) || (cro[v].y - D <= -50) || (cro[v].x - D <= -50))
    29         return true;
    30     else 
    31         return false;
    32 }
    33 
    34 bool DFS ( int V )
    35 { 
    36     bool answer = false;
    37     Visited[V] = true;
    38     if( isSafe(V) ) 
    39         answer = true;
    40     else {
    41         for(int i = 0; i < N; i++) {
    42             if( !Visited[i] && Jump(cro[V].x,cro[V].y,cro[i].x,cro[i].y)) {
    43                 answer = DFS(i);
    44                 if(answer == true)
    45                     break;
    46             }
    47         }
    48     }
    49     return answer;
    50 }
    51 
    52 //第一跳是否可以跳到 是return true 
    53 bool firstJump(int x, int y)
    54 {
    55     if( x*x + y*y <= (7.5+D)*(7.5+D) )
    56         return true;
    57     else
    58         return false;
    59 }
    60 
    61 void save007()
    62 {
    63     bool answer = false;
    64     for(int i = 0; i < N; i++) {
    65         if( !Visited[i] && firstJump(cro[i].x,cro[i].y) ) {
    66             answer = DFS(i);
    67             if( answer == true)
    68                 break;
    69         }
    70     }
    71     if(answer == true)
    72         printf("Yes
    ");
    73     else
    74         printf("No
    ");
    75 }
    76 
    77 
    78 int main()
    79 {
    80     scanf("%d %d",&N,&D);
    81 //    if(N < (35/D - 1)) {    //N至少为 (50-15) /D -1
    82 //        printf("No
    ");
    83 //        return 0;
    84 //    }
    85     for(int i = 0; i < N; i++)
    86         scanf("%f %f",&cro[i].x,&cro[i].y);
    87     save007();
    88     return 0;
    89 }
     
  • 相关阅读:
    C# 关于替换SQL中某个字段的部分内容
    c# sharepoint client object model 创建工作流历史记录
    c# sharepoint client object model 创建文档库
    c# sharepoint client object model 创建列表库
    C# Windows Server 2016 IIS的安装与配置
    C# 判断当前请求是GET还是POST
    SQL 无法执行脚本
    SharePoint 2013 配置 -- 基于表单的身份认证
    C# 浏览器URL地址殊字符转义编码
    C# Session 只有在配置文件或 Page 指令中将 enableSessionState 设置为 true 时,才能使用会话状态。
  • 原文地址:https://www.cnblogs.com/kuotian/p/5375136.html
Copyright © 2011-2022 走看看