Question 201.2022 Winter Ladder Training - 7-9 Rescue 007 (25 points)


Question 201.2022 Winter Ladder Training - 7-9 Rescue 007 (25 points)


1. The topic

insert image description here
insert image description here

2. Problem solving

There are a few points to grasp in this question.
First, how to deal with jumping out of the island at the beginning: although jumping from the shore can definitely be closer to the crocodile and have a better chance to jump, but the shore is a curve, there are countless points, you can't handle it, so we might as well use this as a circle Island, set the jumping point directly at the origin, and then jump one more radius of the island.
Second, how to judge whether I can jump to the next crocodile head: it is all a point, we naturally use the distance formula between two points to get the distance and the distance that can jump, if the former is less than or equal to the latter, then you can jump Third, how to judge the
ability to jump out of the lake: because the lake is square, four straight segments form four sides, you try to jump from the head of the crocodile to the shore, you must want to see if I can jump over when I am closest, And recently it must have been jumping perpendicular to the shore, so I just thought about jumping in the -x direction, +x direction, -y direction, and +y direction to see if there is one that can jump to the shore (more than x or y boundaries).
code show as below:

#include <bits/stdc++.h>

using namespace std;

int N,D;
int visited[101];//标记那个鳄鱼是否被跳过了,即是否点被访问过了
int flag=0;//用于标记007是否有救

struct Point
{
    
    
    int x;
    int y;
}P[101];//开个结构体数组存点坐标

double getD(Point p1,Point p2)//计算两点间距离
{
    
    
    return sqrt(pow(p1.x-p2.x,2)+pow(p1.y-p2.y,2));
}

void dfs(int v0)//改造dfs算法,传参传下标就好了,有了下标,点不愁拿不到
{
    
    
    Point p0=P[v0];
    visited[v0]=1;//标记访问
    if(p0.x-D<=-50||p0.x+D>=50||p0.y-D<=-50||p0.y+D>=50)//判断是否能跳出池子
    {
    
    
        flag=1;
        return;
    }
    if(p0.x==0&&p0.y==0)//007在原点时
    {
    
    
        for(int i=1;i<=N;i++)
        {
    
    
            if(visited[i]!=1&&getD(P[i],p0)<=D+7.5)//因为中间是个圆形的小岛,所以从小岛岸边起跳相当于从原点起跳,但是跳的距离可以多7.5m(转化成定点起跳)
            {
    
    
                dfs(i);
            }
        }
    }
    else
    {
    
    
        for(int i=1;i<=N;i++)
        {
    
    
            if(visited[i]!=1&&getD(P[i],p0)<=D)//两点间距离小于等于能跳的距离则继续dfs那个点
            {
    
    
                dfs(i);
            }
        }
    }
}

int main()
{
    
    
    cin>>N>>D;
    P[0].x=0;P[0].y=0;
    for(int i=1;i<=N;i++)
    {
    
    
        scanf("%d%d",&P[i].x,&P[i].y);
    }
    dfs(0);
    if(flag)
    {
    
    
        cout<<"Yes"<<endl;
    }
    else
    {
    
    
        cout<<"No"<<endl;
    }
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324346079&siteId=291194637