Question 249. 2022 Winter Holiday Ladder Training-7-13 Rescue 007 (Upgrade) (30 points)


Question 249. 2022 Winter Holiday Ladder Training-7-13 Rescue 007 (Upgrade) (30 points)


1. The topic

insert image description here
insert image description here

2. Problem solving

Before looking at the code of this question, you can take a look at the previous non-upgraded version of Rescue 007. After all, the ideas are basically the same, but here are some condiments for the results to be output. code show as below:

#include <bits/stdc++.h>

using namespace std;

const int maxn=101;

int N;
double D;
int vis[maxn];
int parent[maxn];//parent用于表示跳到i号鳄鱼之前的那只鳄鱼的编号,简称父鳄鱼
vector<int> last;//用于存储所有的可达岸边的最终鳄鱼的编号

struct Point
{
    
    
    double x,y;
    int index;//用于存储鳄鱼的下标,这是方便得到parent
} P[maxn];

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

int judge(Point p)//判断p鳄鱼能否让007上岸
{
    
    
    if(p.x-D<=-50||p.x+D>=50||p.y-D<=-50||p.y+D>=50)
    {
    
    
        return 1;
    }
    else
    {
    
    
        return 0;
    }
}

int bfs(struct Point p0)
{
    
    
    if(judge(p0))//看能否从岛一步到岸上—>最后一个测试点。。。
    {
    
    
        return 1;
    }
    queue<Point> q;
    q.push(p0);
    while(!q.empty())
    {
    
    
        Point tmp=q.front();
        q.pop();
        if(tmp.x==0&&tmp.y==0)
        {
    
    
            for(int i=0; i<N; i++)
            {
    
    
                if(getDistance(tmp,P[i])<=D+7.5&&vis[i]!=1)
                {
    
    
                    q.push(P[i]);
                    vis[i]=1;
                    parent[i]=tmp.index;//设置i的父鳄鱼为tmp鳄鱼的编号
                    if(judge(P[i]))
                    {
    
    
                        last.push_back(i);//通过鳄鱼i可以跳到岸上,i放入last
                    }
                }
            }
        }
        else
        {
    
    
            for(int i=0; i<N; i++)
            {
    
    
                if(getDistance(tmp,P[i])<=D&&vis[i]!=1)
                {
    
    
                    q.push(P[i]);
                    vis[i]=1;
                    parent[i]=tmp.index;
                    if(judge(P[i]))
                    {
    
    
                        last.push_back(i);
                    }
                }
            }
        }
    }
    return last.size();
}

int main()
{
    
    
    cin.tie(0),cout.tie(0);
    cin>>N>>D;
    for(int i=0; i<N; i++)
    {
    
    
        cin>>P[i].x>>P[i].y;
        P[i].index=i;
    }
    struct Point p0;
    p0.x=0,p0.y=0,p0.index=-1;
    if(bfs(p0))
    {
    
    
        stack<Point> res;//最终鳄鱼路径
        for(int i=0; i<last.size(); i++)
        {
    
    
            stack<Point> s;//存储以last为终点鳄鱼的走过的鳄鱼路径
            while(parent[last[i]]!=-1)//往回一直到根鳄鱼,以寻迹
            {
    
    
                s.push(P[last[i]]);
                last[i]=parent[last[i]];
            }
            s.push(P[last[i]]);
            if(res.size()==0||res.size()>s.size())//选取步数最小,即鳄鱼数最少
            {
    
    
                res=s;
            }
            else if(res.size()==s.size()&&getDistance(p0,s.top())<getDistance(p0,res.top()))//步数相同则考虑到第一只鳄鱼最小者
            {
    
    
                res=s;
            }
        }
        cout<<res.size()+1<<endl;//步数等于鳄鱼数+1
        while(!res.empty())
        {
    
    
            cout<<res.top().x<<" "<<res.top().y<<endl;
            res.pop();
        }
    }
    else
    {
    
    
        cout<<0<<endl;
    }
}

Guess you like

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