06-图2 Saving James Bond - Easy Version (25分)

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 NN (100100), the number of crocodiles, and DD, the maximum distance that James could jump. Then NN lines follow, each containing the xy(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、深度优先遍历图,没到达新结点,判断能否从当前结点一步跳到安全区域
2、图中两点间有边的条件是小于bond能跳的半径
3、第一步Bond跳动的距离可以视为加上15(小岛半径)

#include 
#include 

using namespace std;

bool distance(vector > &vec,int node,int i,int D)//是否能从node点跳到i点
{
    int distance_x=vec[node][0]-vec[i][0];
    int distance_y=vec[node][1]-vec[node][1];
    if ((distance_x*distance_x+distance_y*distance_y)<=D*D)
    {
        return true;
    }else
    {
        return false;
    }
}

bool safe(vector > &vec,int node,int D)//当前node点是否能一步跳到安全区域
{
    int x=vec[node][0];
    int y=vec[node][1];
    if (x>= 50-D || x<=-50+D || y>=50-D || y<=-50+D)
    {
        return true;
    }else
    {
        return false;
    }
    
}

bool DFS(vector > &vec,int node,vector &flag,int N,int D)//深度优先遍历图
{
    flag[node]=1;
    if (safe(vec, node, D))
    {
        return true;
    }
    for (int i=1; i<=N; ++i)
    {
        if (distance(vec, node,i,D) && flag[i]==0)
        {
             return DFS(vec, i, flag, N, D);
        }
    }
    return false;
}

int main()
{
    int N=0,D=0;
    cin>>N>>D;
    
    if (D>=50-15)
    {
        cout<<"Yes";
    }else
    {
    
    vector > position(N+1,vector(2));
    for (int i=1; i<=N; ++i)
    {
        cin>>position[i][0]>>position[i][1];
    }
    position[0][0]=0;
    position[0][1]=0;
    vector flag(N+1,0);
    int flag_result=0;
    for (int i=1; i<=N; ++i)
    {
        if (distance(position, i,0,D+15))//岛的半径15,可以视为Bond从岛中心点跳出的第一步有长度15的能力值增加
        {
            if(DFS(position, i, flag, N, D))
            {
                flag_result=1;
                break;
            }
        }
    }
    if (flag_result)
    {
        cout<<"Yes";
    }else
    {
        cout<<"No";
    }
        
    }
}


你可能感兴趣的:(PTA)