06-1. Saving James Bond - Hard Version (30) BFS

06-1. Saving James Bond - Hard Version (30)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue

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

提交代

因为期末了  没时间做  今天抽了时间研究了下 ,这道题你用bfs搜索最短路,但是难在路径输出和如果有多条相同的最短路径,那么要输出第一跳最小的那个 具体将代码注释

最后再说一句  这个孤岛的直径是15.。。 简单版的我直接当成半径用了 居然也能过。。。

#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
#define lson rt<<1,l,MID
#define rson rt<<1|1,MID+1,r
//#define lson root<<1
//#define rson root<<1|1
#define MID ((l+r)>>1)
typedef long long ll;
typedef pair<int,int> P;
const int maxn=1005;
const int base=1000;
const int inf=999999;
const double eps=1e-5;
struct node//保存每个鳄鱼的位置  对应图的节点
{
    double x,y;
    int num;
    double dis;
    bool friend operator<(node a,node b)//定义优先级  后面优先队列 在多多条相同的最短路径里找第一步最小的
    {
        return a.dis<b.dis;
    }
} p[maxn];
double d;
int n;
double dis(double x,double y,double xx,double yy)//判断两点间的距离,用于判断是不是能跳上 即判断是不是邻接的
{
     return d-sqrt((x-xx)*(x-xx)+(y-yy)*(y-yy));
}

bool pan(node s)//判断当前点是否能够跳到岸上
{

   if(fabs(s.x-50)<=d||fabs(s.x+50)<=d||fabs(s.y-50)<=d||(s.y+50)<=d)
        return true;
    return false;
}

double find_small_first(node s)//找开始在孤岛中心能跳到的点
{
     return sqrt(s.x*s.x+s.y*s.y)-7.5;
}

int di[maxn];
int pr[maxn];
int bfs(node s)
{//bfs搜索  不多说
    memset(di,-1,sizeof(di));
    memset(pr,-1,sizeof(pr));
    queue<node> q;
    q.push(s);
    di[s.num]=0;
    int i;
    while(!q.empty())
    {
        node tmp=q.front();
        q.pop();
        if(pan(tmp))
        {
            return tmp.num;
        }
        for( i=1;i<=n;i++)
        {
            if(dis(p[i].x,p[i].y,tmp.x,tmp.y)>=0&&di[p[i].num]==-1)
            {
                q.push(p[i]);
                di[p[i].num]=di[tmp.num]+1;
                pr[p[i].num]=tmp.num;
            }
        }
    }
    return 0;
}

vector<int> get_path(int t)//路径还原函数
{
    vector<int> path;
    for(;t!=-1;t=pr[t])path.push_back(t);
    reverse(path.begin(),path.end());
    return path;
}

int main()
{
    int m,i,j,k,t;
    cin>>n>>d;
    for(i=1; i<=n; i++)
    {
        cin>>p[i].x>>p[i].y;
        p[i].num=i;
    }
    if(d+7.5>=50)//特判  对应测试的最后一个测试点
    {
        printf("1\n");
        return 0;
    }
    priority_queue<node> q;//优先队列 在相同的最短路 里 找第一步最小的
    vector<int> path;
    for(i=1;i<=n;i++)
    {
        double kk=find_small_first(p[i]);

        if(kk>=0&&kk<=d)
        {
            p[i].dis=kk;
            q.push(p[i]);
        }
    }
    int dist;
    int flag=1;
    while(!q.empty())//尝试 从孤岛中心 所能到达的所有点
    {
        node
        tmp=q.top();
        q.pop();
        t=bfs(tmp);
        //cout<<di[t]<<' '<<t<<endl;
        if(flag&&t!=0)
        {
            dist=di[t];
            path=get_path(t);
            flag=0;
        }
        else if(t!=0&&di[t]<=dist)
        {
            path=get_path(t);
            dist=di[t];
        }

    }
    t=path.size();
    if(t==0)
        printf("0\n");
    else
    {
        printf("%d\n",t+1);
        for(i=0;i<t;i++)
            printf("%.0lf %.0lf\n",p[path[i]].x,p[path[i]].y);
    }
    return 0;
}
/*
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



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

*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值