07-图5 Saving James Bond - Hard Version

07-图5 Saving James Bond - Hard Version(30 分)

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

题解:

#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>

#define mod 1000000007
const int INF = 0x7f7f7f7f;
typedef long long ll;
const int maxn = 10050;
using namespace std;

struct node
{
    int num;
    int x;
    int y;
    bool flag;
    int dis;
    int path;
};
struct nn
{
    int num;
    double dis;
};
node vec[105];
bool flag;
float m, n;

bool cmp(nn a, nn b)
{
    return a.dis<b.dis;
}
int dij()
{
    queue<node> que;
    que.push(vec[0]);

    while(!que.empty())
    {
        node next = que.front();
        if(next.flag)
        {
            flag = true;
            return next.num;
        }
        que.pop();
        if(next.x==50 && next.y==50)
        {
            nn aa[105];
            int cnt = 0;
            for(int i = 1; i <= m; i++)
            {
                if(-1 == vec[i].dis && sqrt(((vec[i].x-next.x)*(vec[i].x-next.x)+(vec[i].y-next.y)*(vec[i].y-next.y))) <= n+7.5)
                {
                    vec[i].dis = next.dis+1;
                    vec[i].path = next.num;
                    aa[cnt].num = i;
                    aa[cnt++].dis = sqrt(((vec[i].x-next.x)*(vec[i].x-next.x)+(vec[i].y-next.y)*(vec[i].y-next.y)));
                }
            }
            sort(aa, aa+cnt, cmp);
            for(int i = 0; i < cnt; i++)
                que.push(vec[aa[i].num]);
        }
        else
        {
            for(int i = 1; i <= m; i++)
            {
                if(-1 == vec[i].dis)
                {
                    if(sqrt(((vec[i].x-next.x)*(vec[i].x-next.x)+(vec[i].y-next.y)*(vec[i].y-next.y))) <= n)
                    {
                        vec[i].dis = next.dis+1;
                        vec[i].path = next.num;
                        que.push(vec[i]);
                    }
                }
            }
        }
    }
}

int main()
{

    scanf("%f%f", &m, &n);
    flag = false;
    vec[0].num = 0;vec[0].x = 50; vec[0].y = 50;
    vec[0].flag = false; vec[0].dis = 0; vec[0].path = -1;
    for(int i = 1; i <= m; i++)
    {
        int t1, t2;
        scanf("%d%d", &t1, &t2);
        t1 += 50;
        t2 += 50;
        if(abs(100-t1) <= n || abs(t1) <= n || abs(100-t2)<=n || abs(t2) <= n)
        {
            vec[i].num = i;
            vec[i].x = t1;
            vec[i].y = t2;
            vec[i].flag = true;
            vec[i].dis = -1;
            vec[i].path = -1;
        }
        else
        {
            vec[i].num = i;
            vec[i].x = t1;
            vec[i].y = t2;
            vec[i].flag = false;
            vec[i].dis = -1;
            vec[i].path = -1;
        }
    }
    int k;
    if(n+7.5 >= 50)
    {
        printf("1\n");
        return 0;
    }
    else
    {
        k = dij();
    }
    if(flag)
    {
        printf("%d\n", vec[k].dis + 1);
        int dd[105];
        int i;
        dd[0] = k;
        for(i = 1; -1 != vec[k].path; i++)
        {
            dd[i] = vec[k].path;
            k = vec[k].path;
        }
        for(int j = i - 2; j >=0 ; j--)
        {
            printf("%d %d\n", vec[dd[j]].x-50, vec[dd[j]].y-50);
        }
    }
    else
        printf("0\n");
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值