06-图2 Saving James Bond - Easy Version

这篇博客介绍了如何利用BFS(广度优先搜索)和DFS(深度优先搜索)算法解决一个电影场景中的问题:詹姆斯邦德在被鳄鱼包围的湖中试图逃生。博主陈越提出了一个结构体表示图的方法,并通过编程实现判断邦德是否能跳到岸边。代码中考虑了人与岸边、人与鳄鱼、鳄鱼与鳄鱼以及鳄鱼与岸边之间的距离比较,最后通过DFS进行遍历判断邦德能否逃脱。
摘要由CSDN通过智能技术生成

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

Sample Output 1:

Yes

Sample Input 2:

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

Sample Output 2:

No

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

思路:

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

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

AC代码:

#include<bits/stdc++.h>
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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值