Saving James Bond - Easy Version

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)N (≤100), the number of crocodiles, and D, 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

分析

总体算法:DFS

void ListComponents(Graph G)
{
    for(each V in G)
    if(!visited[V])
    DFS(V);
}
void DFS(Vertex V)
{
    visited[V]=true;
    for(V的每个邻接点W)
    if(!visited[W])DFS(W);
}

由DFS算法写出本道题的基本算法框架(伪代码)

void Save007(Graph G)
{
    for(each in G)
    {
        if(!visited[V]&&FirstJump(V))
        {
            answer=DFS(V);
            if(answer==YES)break;
        }
    }
    if(answewr==YES) output("Yes");
    else output("No");
}
int DFS(Vertex V)
{
    visited[V]=true;
    if(IsSafe(V))answer=YES;
    else{
        for(each W in G)
            if(!visited[W]&&Jump(V,W)){
                answer=DFS(W);
                if(answer==YES)break;
            }
    }
    return answer;
}

ac代码

(别嫌弃,毕竟初学者)

#include<iostream>
#include<cmath>
#define MaxCorcodiles 101
#define Diameter 15
using namespace std;
typedef struct Point{
    int x,y;
}point;
double isEdge(point,point,int);
bool canescape(point,int);
void BuildMGraph(int,int,point*,double[][MaxCorcodiles]);
bool DST(int,int,int,bool*,point*,double[][MaxCorcodiles]);
int main(int argc,char*argv[]) {
    int n,jump;
    bool answer= false,Vistied[MaxCorcodiles];
    double MGraph[MaxCorcodiles][MaxCorcodiles];
    point Datapoint[MaxCorcodiles];
    cin>>n>>jump;
    for(int i=0;i<n;i++)for(int j=0;j<n;j++)MGraph[i][j]=-1;
    BuildMGraph(n,jump,Datapoint,MGraph);
    for(int i=0;i<n;i++)Vistied[i]=false;
    for(int i=0;i<n;i++)if(sqrt(pow(Datapoint[i].x,2)+pow(Datapoint[i].y,2))<=jump+Diameter) {
    answer = DST(i, n, jump, Vistied, Datapoint, MGraph);
    if(answer)break;
    }
    if(answer)cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
}
bool canescape(point p,int jump){
    return (p.x-jump<=-50||p.x+jump>=50||p.y+jump>=50||p.y-jump<=-50);
}
double isEdge(point p1,point p2,int jump){
    double weight =sqrt(pow(p1.x - p2.x,2) + pow(p1.y - p2.y,2));
    if(weight<=jump)return weight;
    else return -1;
}
void BuildMGraph(int n,int jump,point* Datapoint,double MGraph[][MaxCorcodiles]){
    double w;
    for(int i=0;i<n;i++)cin>>Datapoint[i].x>>Datapoint[i].y;
    for(int i=0;i<n;i++)for(int j=0;j<n;j++)
    if(i!=j&&(w=isEdge(Datapoint[i],Datapoint[j],jump))>=0)MGraph[i][j]=w;
}
bool DST(int v,int n,int jump,bool*Vistied,point*Datapoint,double MGraph[][MaxCorcodiles]){
    bool answer = false;
    Vistied[v]= true;
    if(canescape(Datapoint[v],jump))answer=true;
    for(int i=0;i<n;i++)if(MGraph[v][i]>0&&!Vistied[i]){
        answer=DST(i,n,jump,Vistied,Datapoint,MGraph);
        if(answer)break;
    }
    return answer;
}