7-11 Saving James Bond - Easy/Hard Version (30 分) C语言实现

封校宅在寝室,开始肝图论算法题。题目思路如图,就是007在岛中,需要踩鳄鱼头(图中蓝点点)跳到岸。

坑点

来搜csdn的一般都是卡测试点了,我简要说下这两题容易出现的错误(我自己遇到的):

首先这两题共同的就是,需要注意边界条件,可以把这个lake假象成一个框框,要注意跳到框框边缘部分也是属于安全地带!也就是说遇到<=-50或者>=50,都可以算safe了,所以可以检查一下自己的等于号有没有用错。

hard version的坑点在于:相比于easy需要考虑的更多了,写hard version的时候我才意识到自己easy应该忽略了很多条件,但奇迹般的AC了,主要是因为不考虑这些条件也影响输出结果。

这道题我采用了BFS+队列实现

容易出错的测试点:

测试点3:在记录鳄鱼的位置的时候要注意筛去那些在岛上的鳄鱼和在岸上的鳄鱼

测试点4:!我就是被这个卡了,要看清题意,

 If there are many shortest paths,just output the one with the minimum first jump

就是说在跳数一样的时候,要输出第一跳路径最短的而非进来序号最小的,所以一开始能第一跳的鳄鱼入队列之后需要根据路径长度排个序!!!我采用了qsort实现。

测试点5:就是说007腿很长,一跳就可以跳到岸,需要判断下,此时输出1。

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

Hard 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 a shortest path to reach one of the banks. The length of a path is the number of jumps that James has to make.

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, if James can escape, output in one line the minimum number of jumps he must make. Then starting from the next line, output the position (x,y) of each crocodile on the path, each pair in one line, from the island to the bank. If it is impossible for James to escape that way, simply give him 0 as the number of jumps. If there are many shortest paths, just output the one with the minimum first jump, which is guaranteed to be unique.

Sample Input 1:

17 15
10 -21
10 21
-40 10
30 -50
20 40
35 10
0 -10
-25 22
40 -40
-30 30
-10 22
0 11
25 21
25 10
10 10
10 35
-30 10

结尾无空行

Sample Output 1:

4
0 11
10 21
10 35

结尾无空行

Sample Input 2:

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

Sample Output 2:

0

代码实现

写的代码并不是最好,有冗余,但是测试通过了。

Easy Version:

#include <stdio.h>
#include <math.h>
#define diameter 15
//不需要邻接矩阵,直接建结构数组
int maxdist;
int n;
typedef struct cr{
    int x;
    int y;
}cro;
cro cros[101];
int visited[101];

int IsSafe(int i){
    int x=cros[i].x;
    int y=cros[i].y;
    if(x-maxdist<=-50||x+maxdist>=50||y-maxdist<=-50||y+maxdist>=50)return 1;
    else return 0;
}
void Creategraph(){
    for(int i=0;i<n;i++){
        scanf("%d%d",&cros[i].x,&cros[i].y);
    }
}
int FirstJump(int v){
    double dist=pow(cros[v].x,2)+pow(cros[v].y,2);
    return (dist<=pow(diameter/2+maxdist,2)?1:0);
}
int Jump(int v,int w){
    double dist=pow(cros[v].x-cros[w].x,2)+pow(cros[v].y-cros[w].y,2);
    return dist<=pow(maxdist,2)?1:0;
}
int DFS(int v){
    int w;
    visited[v]=1;
    int ret=0;
    if(IsSafe(v))return 1;
    else{
        for(w=0;w<n;w++){
            if(!visited[w]&&Jump(v,w)){
                ret=DFS(w);
                if(ret)return 1;
            }
        }
    }
    return ret;
}

int main(){
    int d;
    int ans=0;
    scanf("%d%d",&n,&d);
    maxdist=d;
    Creategraph();
    int v;
    for(v=0;v<n;v++){
        if(!visited[v]&&FirstJump(v)){
            ans=DFS(v);
            if(ans)break;
        }
    }
    if(ans)puts("Yes");
    else puts("No");
    return 0;
}

Hard Version:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

#define diameter 15
int N,D;
int flag=0;
struct Location{
    int x,y;
}loc[101];
int path[101];
int root[101];
int queue[101];
int hh=-1,tt=-1;

int compare(const void*a,const void *b){
    int *pa=(int*)a;
    int *pb=(int*)b;
    int n1=*pa;
    int n2=*pb;
    int dist1=pow(loc[n1].x,2)+pow(loc[n1].y,2);
    int dist2=pow(loc[n2].x,2)+pow(loc[n2].y,2);
    return dist1-dist2;
}

int IsRight(int x,int y){
    if(y>=50||y<=-50||x>=50||x<=-50)return 0;
    if((x*x+y*y)<=pow((diameter/2),2))return 0;
    return 1;
}

int IsSafe(int i){
    int x=loc[i].x;
    int y=loc[i].y;
    if(x-D<=-50||x+D>=50||y-D<=-50||y+D>=50){
        flag=1;
        return 1;
    }
    else return 0;
}

int FirstJump(int i){
    int x=loc[i].x;
    int y=loc[i].y;
    double dist=x*x+y*y;
    if(dist<=pow((D+diameter/2),2))return 1;
    else return 0;
}

int Jump(int i,int j){
    int x1=loc[i].x;
    int y1=loc[i].y;
    int x2=loc[j].x;
    int y2=loc[j].y;
    double dist=pow(x1-x2,2)+pow(y1-y2,2);
    if(dist<=pow(D,2))return 1;
    else return 0;
}

void PrintPath(int idx){
    int x,y,i;
    int cnt=1;
    for(i=idx;path[i]!=0;i=path[i]){
        root[cnt++]=i;
    }
    root[cnt]=i;
    printf("%d\n",cnt+1);
    for(int j=cnt;j>0;j--){
        i=root[j];
        x=loc[i].x;
        y=loc[i].y;
        printf("%d %d",x,y);
        puts("");
    }
}

int BFS(){
    memset(path,-1,sizeof path);
    tt=-1,hh=-1;
    int idx,x,y,i;
    for(i=0;i<N;i++){
        if(FirstJump(i))
        {queue[++tt]=i;
        path[i]=0;
        }
    }
    qsort(queue,tt,sizeof(int),compare);
    while(hh<tt){
        idx=queue[++hh];
        if(IsSafe(idx)){
            PrintPath(idx);
            return 1;
        }
        x=loc[idx].x;
        y=loc[idx].y;
        for(int j=0;j<N;j++){
            if(idx!=j && path[j]==-1 && Jump(idx,j)){
                queue[++tt]=j;
                path[j]=idx;
            }
        }
    }
    return 0;
}

int main(){
    int i,j=1;
    int x,y;
    scanf("%d%d",&N,&D);
    for(i=0;i<N;i++){
        scanf("%d%d",&x,&y);
        if(IsRight(x,y)){
            loc[j].x=x;
            loc[j].y=y;
            j++;
        }
    }
    N=j+1;
    if(IsSafe(0))puts("1");
    else BFS();
    if(flag==0)puts("0");
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值