zoukankan      html  css  js  c++  java
  • 7-11 Saving James Bond

    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
    ",num);
        else {
            getpath(q[d].l,num + 1);
            printf("%d %d
    ",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;
    }
  • 相关阅读:
    自己定义九宫格手势解锁
    2015程序猴的总结:不破楼兰终不还!
    Codeforces Round #402 (Div. 2)
    [整体二分]【学习笔记】【更新中】
    BZOJ 3110: [Zjoi2013]K大数查询 [整体二分]
    BZOJ 2738: 矩阵乘法 [整体二分]
    BZOJ 2527: [Poi2011]Meteors [整体二分]
    [偏序关系与CDQ分治]【学习笔记】
    BZOJ 2244: [SDOI2011]拦截导弹 [CDQ分治 树状数组]
    COGS 2479. [HZOI 2016]偏序 [CDQ分治套CDQ分治 四维偏序]
  • 原文地址:https://www.cnblogs.com/8023spz/p/12264160.html
Copyright © 2011-2022 走看看