7-11 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 (≤), the number of crocodiles, and D, the maximum distance that James could jump. Then N lines follow, each containing the ( 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 ( 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时的上一个点,这样可以找到路径,另外对于存在多种解的情况,要求输出第一跳最小的情况,那就在找到所有第一跳后先按距离升序排序在进行bfs。代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct crocodile {
    int x,y,l;
}c[100],q[100],temp;
int n,d,head,tail,flag;
int check(int x,int y) {
    x = abs(x);
    y = abs(y);
    return x + d >= 50 || y + d >= 50;
}
void getpath(int d,int num) {
    if(d == -1) printf("%d\n",num);
    else {
        getpath(q[d].l,num + 1);
        printf("%d %d\n",q[d].x,q[d].y);
    }
}
int cmp(const void *a,const void *b) {
    struct crocodile aa = *(struct crocodile *)a,bb = *(struct crocodile *)b;
    return pow(aa.x,2) + pow(aa.y,2) - pow(bb.x,2) - pow(bb.y,2);
}
int main() {
    scanf("%d%d",&n,&d);
    for(int i = 0;i < n;i ++) {
        scanf("%d%d",&c[i].x,&c[i].y);
        if(sqrt(pow(c[i].x,2) + pow(c[i].y,2)) - 7.5 <= d) {
            c[i].l = -1;
            q[tail ++] = c[i];
            i --,n --;
        }
    }
    qsort(q,tail,sizeof(q[0]),cmp);
    while(head < tail) {
        temp = q[head ++];
        if(check(temp.x,temp.y)) {
            flag = head - 1;
            break;
        }
        for(int i = 0;i < n;i ++) {
            if(sqrt(pow(c[i].x - temp.x,2) + pow(c[i].y - temp.y,2)) <= d) {
                c[i].l = head - 1;
                q[tail ++] = c[i];
                c[i --] = c[-- n];
            }
        }
    }
    if(flag) getpath(flag,1);
    else printf("%d",d + 7.5 >= 50 ? 1 : 0);//一步就可达的情况 或 不可达
    return 0;
}

原文地址:https://www.cnblogs.com/8023spz/p/12264160.html

时间: 2024-10-10 01:09:01

7-11 Saving James Bond - Hard Version (30分)的相关文章

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

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 lan

PTA 06-图2 Saving James Bond - Easy Version (25分)

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 crocodile

06-图2 Saving James Bond - Easy Version (25 分)

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 crocodile

07-图4. Saving James Bond - Hard Version (30)

本题测试点5是从小岛范围内可以直接跳到岸边-- 测试点4是验证步数第一跳最小的情况,刚开始没有考虑回溯,所以错了-- #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #in

06-图4. Saving James Bond - Hard Version (30)

该题的关键是题干最后一句:如果有不同的路线都是最短路,那么输出第一跳最短的路线.解决方法是把湖心小岛周围一圈的节点(即距离湖心小岛小于D的鳄鱼)看作起点,对这些起点依次调用无权最短路算法,找出路径最短(且相同)的那些起点,然后比较从湖心小岛到它们的距离,输出最小的距离所在的路径即可. #include <iostream> #include <vector> #include <queue> #include <stack> #include <cma

pat05-图2. Saving James Bond - Easy Version (25)

05-图2. Saving James Bond - Easy Version (25) 时间限制 200 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 capture

PAT Saving James Bond - Easy Version

Saving James Bond - Easy Version 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

Saving James Bond - Hard Version(PAT)

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

05-图2. Saving James Bond - Easy Version (25)

05-图2. Saving James Bond - Easy Version (25) 时间限制 200 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 capture