06-图2 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), 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

 0到Nv-1视为一个图(岛不包括在内),可能不是连通的,要每个连通集分别遍历,因为从center岛上跳到鳄鱼上是不一样的

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#define MaxVertexNum 101
typedef int Vertex;

struct DataType{
    int x;
    int y;
    bool Visited;
}Data[MaxVertexNum];
int Nv,D;

int CalcuDistance(Vertex V1,Vertex V2){
    return sqrt( pow(Data[V1].x-Data[V2].x,2)+pow(Data[V1].y-Data[V2].y,2) );
}

bool IsSafe(Vertex V){
    int x,y;
    x=Data[V].x;y=Data[V].y;
    if(x+50<=D || 50-x<=D || y+50<=D || 50-y<=D)    return true;
    else    return false;
}


bool DFS(Vertex V){
    Data[V].Visited=true;
    
    if(IsSafe(V))   return true;
    else return false;
    
    int CanJump=D;

    Vertex W;
    bool answer=false;
    for(W=0;W<Nv;W++){
        if(CalcuDistance(V,W)<=CanJump && !Data[W].Visited){
            answer=DFS(W);
            if(answer)  break;
        }
    }
    if(answer)  return true;
    else    return false;
}

int main()
{
    int i;
    
    scanf("%d %d",&Nv,&D);
    for(i=0;i<Nv;i++){
        scanf("%d %d",&Data[i].x,&Data[i].y);
    }
    
    for(i=0;i<Nv;i++)   Data[i].Visited=false;
    
    Data[Nv].x=0;Data[Nv].y=0;//设为center
    
    bool answer;
    
    for(i=0;i<Nv;i++){
        if(CalcuDistance(Nv,i)<=D+15 && !Data[i].Visited){
            answer=DFS(i);
            if(answer)  break;
        }
    }
    if(answer)  puts("Yes");
    else    puts("No");
    return 0;
}

最小跳,人工乱序这个测试点没过,不知道是什么原因 ,to be continued

突然发现题目的直径是diameter15,不是半径。。。于是我把15改成了7.5

    for(i=0;i<Nv;i++){
        if(CalcuDistance(Nv,i)<=D+7.5 && !Data[i].Visited){
            answer=DFS(i);
            if(answer)  break;
        }
    }

 结果错得更多了。。。。究竟是什么原因呢

把center岛也作为节点(下标为0)的版本:

犯过一个错误,是因为如果这样写的话,center岛被设为Visited之后不能再返回了,而正在遍历的连通集外的鳄鱼跳不到了,但是如果能回到岛上就能跳上去。 暂时还没找到什么原因。。

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#define MaxVertexNum 105
typedef int Vertex;

struct DataType{
    int x;
    int y;
    bool Visited;
}Data[MaxVertexNum];
int Nv,D;

int CalcuDistance(Vertex V1,Vertex V2){
    return sqrt( pow(Data[V1].x-Data[V2].x,2)+pow(Data[V1].y-Data[V2].y,2) );
}

bool IsSafe(Vertex V){
    int x,y;
    x=Data[V].x;y=Data[V].y;
    if(x+50<=D || 50-x<=D || y+50<=D || 50-y<=D)    return true;
    else    return false;
}

bool IsFirstJump=true;
bool DFS(Vertex V){
    Data[V].Visited=true;
    
    if(IsSafe(V))   return true;
    else return false;
    
    int CanJump=D;
    if(V==0) IsFirstJump=true;
    if(IsFirstJump){
        CanJump+=15;
        IsFirstJump=false;
    }
    
    Vertex W;
    bool answer;
    for(W=0;W<Nv;W++){
        if(CalcuDistance(V,W)<=CanJump && !Data[W].Visited){
            answer=DFS(W);
            if(answer)  break;
        }
    }
    if(answer)  return true;
    else    return false;
}

int main()
{
    int i;
    
    scanf("%d %d",&Nv,&D);
    Nv++;
    Data[0].x=0;Data[0].y=0;
    for(i=1;i<Nv;i++){
        scanf("%d %d",&Data[i].x,&Data[i].y);
    }
    
    for(i=0;i<Nv;i++)   Data[i].Visited=false;
    
    bool answer;
    for(i=0;i<Nv;i++){
        answer=DFS(i);
        if(answer)  break;
    }
    if(answer)  puts("Yes");
    else    puts("No");
    return 0;
}

改了一下,从岛开始DFS,DFS(0):

错了

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值