pta 06-图2 Saving James Bond - Easy Version (25分) C语言实现

11 篇文章 1 订阅

基本题意

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

基础想法

首先这是个很常见的图的遍历问题,这个问题深度,广度均可以实现,但是深度要好点,代码实现思路,很多来自mooc网上的,浙江大学的《数据结构》。大家可以去看看,很棒的网课。

具体代码

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define MAX 100
#define false 0
#define true 1
struct Eyu
{
    int x;
    int y;
}L[MAX];/*储存这个节点的坐标*/
int tiaoyue,geshu;/*节点个数,跳跃距离*/
int visited[MAX]={0};/*标记数组*/
int jump(int v1 ,int v2)/*普通跳跃,不包含第一跳,和上岸那一跳*/
{
    int i,j;
    i=pow((L[v1].x-L[v2].x),2);
    j=pow((L[v1].y-L[v2].y),2);
    i=i+j;
    j=pow(tiaoyue,2);
    if(j>=i)
        return 1;
    else
        return 0;
}
int SHANAN(int v)/*上岸一条*/
{
    int a,b;
    a=abs(L[v].x);
    b=abs(L[v].y);
    if(a+tiaoyue>=50)
        return 1;
    if(b+tiaoyue>=50)
        return 1;
    return 0;
}
int DFS(int v)/*深度优先搜索,改良的*/
{
    int answer = false;
    visited[v] = 1;
    if(SHANAN(v))
    {
        answer = true;/*目的在遍历中,找出是否有一个点可以上岸*/
    }
    else
    {
        for(int i=0; i<geshu; i++)
        {
            if((!visited[i]) &&(jump(v,i)) )
            {
                answer = DFS(i);
            }
            if(answer == true)
            {
                break;
            }
        }
    }
    return answer;
}
int diJump(int v)/*第一跳*/
{
    int i,j;
    i=pow(L[v].x,2);
    j=pow(L[v].y,2);
    i=i+j;
    j=pow(tiaoyue+15/2,2);
    if(j>=i)
        return 1;
    else
        return 0;
}
void ZHU()
{
    int m,i;
    for(i=0;i<geshu;i++)
    {
        if(!visited[i]&&diJump(i))/*满足没遍历,且第一跳距离满足*/
        {
            m=DFS(i);
            if(m==1)
                break;/*找到就返回*/
        }
    }
    if(m==1)
        printf("Yes\n");/*输出*/
    else
        printf("No\n");

}
int main()
{
    int i,j;
    scanf("%d %d",&geshu,&tiaoyue);
    for(i=0;i<geshu;i++)
    {
        scanf("%d %d",&L[i].x,&L[i].y);/*输入*/
    }
    ZHU();
    return 0;
}


结果

在这里插入图片描述学习进步中,大家一起,代码有不足的地方,请指出,一起改正进步。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值