• 06-图2 Saving James Bond - Easy Version


    06-图2 Saving James Bond - Easy Version

    分数 25
    作者 陈越
    单位 浙江大学

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    Sample Output 1:

    Yes
    
    • 1

    Sample Input 2:

    4 13
    -12 12
    12 12
    -12 -12
    12 -12
    
    • 1
    • 2
    • 3
    • 4
    • 5

    Sample Output 2:

    No
    
    • 1

    代码长度限制
    16 KB
    时间限制
    400 ms
    内存限制
    64 MB
    C++ (g++)

    思路:

    本题亮点,用结构体表示图。
    先放陈越姥姥的讲解视频,在这儿~

    bfs算法改进:
    如果能到达岸边,直接返回true
    否则置访问标志,然后对该结点进行深度优先遍历。
    设置人为0,鳄鱼为1~n,岸边为n+1;
    //人与岸边
    岸边-小岛半径与d比较
    //人与鳄鱼
    人与鳄鱼的距离与d比较
    //鳄鱼与鳄鱼
    鳄鱼与鳄鱼的距离与d比较
    //鳄鱼与岸边
    鳄鱼与岸边距离的绝对值与d比较

    AC代码:

    #include
    using namespace std;
    int G[300][300];
    int vis[300];
    struct location{
        int x;
        int y;
    }loc[100];
    
    float dic(location a,location b){
        return pow(pow(a.x-b.x,2)+pow(a.y-b.y,2),0.5);
    }
    
    bool DFS(int cur,int n){
        if(cur==n+1) return true;
        vis[cur]=1;
        bool flag=false;
        int i;
        for(i=1;i<=n+1;i++){
            if(!vis[i]&&G[cur][i]){
                flag=DFS(i,n);
                if(flag) return true;
            }
        }
        if(i==n+2) return false;
    }
    
    int main(){
        memset(vis,0,sizeof(vis));
        int n,d;
        cin>>n>>d;
        //设置人为0,鳄鱼为1~n,岸边为n+1
        loc[0].x=loc[0].y=0;
        
        //人与岸边,可不可以直接跳过去
        if(50-7.5<=d){
            cout<<"Yes"<<endl;
            return 0;
        }
        //鳄鱼与人的边
        for(int i=1;i<=n;i++){
            cin>>loc[i].x>>loc[i].y;
            if(dic(loc[0],loc[i])-7.5<=d)
                G[0][i]=G[i][0]=1;
        }
        //鳄鱼与鳄鱼
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                if(dic(loc[i],loc[j])<=d||i==j)
                    G[i][j]=G[j][i]=1;
            }
        }
        //鳄鱼与岸边
        for(int i=1;i<=n;i++){
            if(abs(abs(loc[i].x)-50)<=d||abs(abs(loc[i].y)-50)<=d)
                G[i][n+1]=G[n+1][i]=1;
        }
        if(DFS(0,n))
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
  • 相关阅读:
    【半监督学习】CNN与Transformer的结合
    CMake编译命令笔记
    【LeetCode:2316. 统计无向图中无法互相到达点对数 | BFS + 乘法原理】
    callee和caller
    actionBar 导航栏学习
    vue3 表单搜索内容回显到地址栏
    vue 中引入 jquery
    LeetCode——动态规划篇(二)
    MIT 6.S081学习笔记(第三章)
    vue3学习笔记(2)
  • 原文地址:https://blog.csdn.net/manerzi/article/details/128034132