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

题意:

007从一个100*100的正方形湖中心的小岛上,小岛是直径为15的圆,007所在位置左边为(0,0),给出鳄鱼坐标和007的最大跳跃距离,问007能不能跳到岸上。

思路:

1.以邻接矩阵存储图
1.将007、岸边、鳄鱼看作n+2个节点,
2.建图
1.根据节点(1--n)之间的距离是否<=d,判断鳄鱼间是否有边 
2.判断节点和岸边的最短矩阵是否<=d,判断鳄鱼和岸边是否有边
3.判断007和岸边的最短距离是否<=(d-7.5),判断007能不能直接到岸边
4.判断007和鳄鱼的距离是否<=(d-7.5),判断007能不能跳到鳄鱼上
3.DFS寻找007和岸边是否存在通路
#include<iostream>
#include<cstdio>
#include<deque>
#include<cmath>
using namespace std; 
/*
1.将007、岸边、鳄鱼看作n+2个节点,
2.根据节点之间的距离是否<=d,判断节点间是否有边 
3.DFS寻找007和岸边是否存在通路 
*/
const int MAX_N = 102;
struct location{
    int x;
    int y;
}loc[MAX_N*MAX_N];
int g[MAX_N][MAX_N];
int visited[MAX_N];
inline float dis(struct location a, struct 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;
    visited[cur]=1;
    int i;
    bool flag=false;
    for(i = 1;i<n+2;i++){
        if(visited[i]==0&&g[cur][i]==1){
            flag = DFS(i,n);
            break;
        }
    }
    if(i==n+2)
        return false;
    else if(flag){
        return true;
    }else{
        DFS(cur,n);
    }   
} 
int main(){
    int n,d;
    cin>>n>>d;
    for(int i = 1; i <= n;i++){
        cin>>loc[i].x>>loc[i].y;
    }

    //建图,图中0代表007,1-n代表鳄鱼,n+1代表岸边 
    for(int i = 1; i <= n; i++){
        //建立鳄鱼和河岸的联系
        if(50-loc[i].x<=d || loc[i].x+50<=d||loc[i].y+50<=d||50-loc[i].y<=d){
            g[n+1][i] = 1;
            g[i][n+1] = 1;
        } 
    } 
    for(int i = 1; i <= n; i++){
        //建立007和鳄鱼的联系
        if(dis(loc[0],loc[i])-7.5<=d){
            g[0][i] = 1;
            g[i][0] = 1;
        } 
    } 
        //建立鳄鱼和鳄鱼的联系
    for(int i = 1; i <= n; i++){
        for(int j = i; j <= n; j++){
            if(dis(loc[i],loc[j])<=d||i==j){
                g[i][j]=1;
                g[j][i]=1;
            } 
        }
    } 
    //从0出发,如果能到达n+1,则yes,否则no 
    if(DFS(0,n))
        cout<<"Yes"<<endl;
    else
        cout<<"No"<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37513086/article/details/80015302