拯救007(升级版) (30分)

在老电影“007之生死关头”(Live and Let Die)中有一个情节,007被毒贩抓到一个鳄鱼池中心的小岛上,他用了一种极为大胆的方法逃脱 —— 直接踩着池子里一系列鳄鱼的大脑袋跳上岸去!(据说当年替身演员被最后一条鳄鱼咬住了脚,幸好穿的是特别加厚的靴子才逃过一劫。)
设鳄鱼池是长宽为100米的方形,中心坐标为 (0, 0),且东北角坐标为 (50, 50)。池心岛是以 (0, 0) 为圆心、直径15米的圆。给定池中分布的鳄鱼的坐标、以及007一次能跳跃的最大距离,你需要给他指一条最短的逃生路径 —— 所谓“最短”是指007要跳跃的步数最少。

输入格式:
首先第一行给出两个正整数:鳄鱼数量 N(≤100)和007一次能跳跃的最大距离 D。随后 N 行,每行给出一条鳄鱼的 (x,y) 坐标。注意:不会有两条鳄鱼待在同一个点上。

输出格式:
如果007有可能逃脱,首先在第一行输出007需要跳跃的最少步数,然后从第二行起,每行给出从池心岛到岸边每一步要跳到的鳄鱼的坐标 (x,y)。如果没可能逃脱,就在第一行输出 0 作为跳跃步数。如果最短路径不唯一,则输出第一跳最近的那个解,题目保证这样的解是唯一的。

输入样例 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

输出样例 1:

4
0 11
10 21
10 35

输入样例 2

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

输出样例 2:

0

这题数据比较小用一维的vec每次遍历就可以实现,如果是大数据的话就要用二维 在读入的时候判断距离构图,用的是dfs遍历,标记和路径要回溯,形参跟随当前结构体,步数和第一步的距离。不过最大N的那个测试点没过,我也不知道为啥,希望大神能指教

#include<bits/stdc++.h>
using namespace std;
#define N 110
#define INF 0x3f3f3f
struct zb{
    int x,y;
};
int n;
double m;
vector<zb>vec;
stack<zb>sta,staend,temp;
int vis[N][N];
int minnum=INF;

double deal(zb a,zb b){
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

bool judge(zb t){
    if(50-abs(t.x)<=m)
        return true;
    if(50-abs(t.y)<=m)
        return true;
    return false;
}
int F;
void dfs(zb t,int fnum,int first)
{
    if(judge(t)){
        if(fnum+1<minnum){
            minnum=fnum+1;
            staend=sta;
            F=first;
        }
        else if(fnum+1==minnum){
            if(first<F){
                F=first;
                staend=sta;
            }
            else{
                return ;
            }
        }
        return ;
    }
    for(int i=0;i<vec.size();i++){
        if(!vis[vec[i].x+50][vec[i].y+50]&&deal(t,vec[i])<=m){
            vis[vec[i].x+50][vec[i].y+50]=1;
            sta.push(vec[i]);
            if(t.x==0&&t.y==0)
                dfs(vec[i],fnum+1,deal(t,vec[i]));
            else
                dfs(vec[i],fnum+1,first);
            sta.pop();
            vis[vec[i].x+50][vec[i].y+50]=0;
        }
    }
}

int main()
{
    cin>>n>>m;
    for(int i=0;i<n;i++){
        zb t;cin>>t.x>>t.y;
        vec.push_back(t);
    }
    zb t={0,0};
    dfs(t,0,0);
    if(minnum==INF)
        cout<<'0'<<endl;
    else {
        cout << minnum << endl;
        while(!staend.empty()){
            zb t=staend.top();
            temp.push(t);
            staend.pop();
        }
        while(!temp.empty()){
            zb t=temp.top();
            cout<<t.x<<' '<<t.y<<endl;
            temp.pop();
        }
    }
    return 0;
}
发布了38 篇原创文章 · 获赞 5 · 访问量 861

猜你喜欢

转载自blog.csdn.net/Fooooooo/article/details/103642947