zoukankan      html  css  js  c++  java
  • 07-图5 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(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<cstdio>
    #include<queue>
    #include<stack>
    #include<cmath>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int maxn = 110;
    const int minLen = 50 - 15.0/2;
    
    struct Point
    {
        int x,y;
    }point[maxn];
    
    int path[maxn] = {0};
    bool vis[maxn] = {0};
    int n,d;
    
    void BFS();
    void init(int b[]);
    bool cmp(int x,int y);
    int FirstJump(int v);
    bool isSafe(int v);
    bool Jump(int v1,int v2);
    
    int main()
    {
        scanf("%d%d",&n,&d);
        for (int i = 0; i < n; i++)
        {
            scanf("%d%d",&point[i].x,&point[i].y);
        }
        
        if (d >= minLen)
        {
            printf("1
    ");
        }
        else
        {
            BFS();
        }
        return 0;
    }
    
    void BFS()
    {
        int b[maxn];
        init(b);
        sort(b,b+n,cmp);
        
        queue<int> q;
        int last;
        int tail;
        int step = 2;
        
        for (int i = 0; i < n; i++)
        {
            if(FirstJump(b[i]))
            {
                q.push(b[i]);
                vis[b[i]] = true;
                last = b[i];
            }
        }
        
        while(!q.empty())
        {
            int now = q.front();
            q.pop();
            
            if (isSafe(now))
            {
                int k =1;
                stack<int> s;
                cout << step << endl;
                
                while (k < step)
                {
                    s.push(now);
                    now = path[now];
                    k++;
                }
                
                while (!s.empty())
                {
                    now = s.top();
                    s.pop();
                    cout << point[now].x << " " << point[now].y << endl;
                }
                return;
            }
            
            for (int i = 0; i < n; i++)
            {
                if (!vis[i] && Jump(now,i))
                {
                    q.push(i);
                    vis[i] = true;
                    tail = i;
                    path[i] = now;
                }
            }
            
            if (last == now)
            {
                last = tail;
                step++;
            }
        }
        
        if (q.empty())
        {
            cout << "0" << endl;
        }
    }
    
    void init(int b[])
    {
        for (int i = 0; i < n; i++)
        {
            b[i] = i;
        }
    }
    
    bool cmp(int x,int y)
    {
        return FirstJump(x) < FirstJump(y);
    }
    
    int FirstJump(int v)
    {
        int dis = pow(point[v].x,2) + pow(point[v].y,2);
        int dis_Jump = pow(15.0/2 + d,2);
        if (dis <= dis_Jump)
        {
            return dis;
        }
        else
        {
            return 0;
        }
    }
    
    bool isSafe(int v)
    {
        int dis_safe = 50 - d;
        return abs(point[v].x) >= dis_safe || abs(point[v].y) >= dis_safe;
    }
    
    bool Jump(int v1,int v2)
    {
        int dis_x = pow(point[v1].x-point[v2].x, 2);
        int dis_y = pow(point[v1].y-point[v2].y, 2);
        if (dis_x + dis_y <= d*d)
        {
            return true;
        }
        else
        {
            return false;
        }
    }


  • 相关阅读:
    天兔(Lepus)监控系统慢查询分析平台安装配置
    java怎么用一行代码初始化ArrayList
    yum命令不能使用的相关错误
    【转】Android APP性能测试
    【转】Java经典问题算法大全
    [转]java中Map,List与Set的区别
    关于编写性能高效的javascript事件的技术
    ESLint 规则
    HTML5 使用application cache 接口实现离线数据缓存
    qunit 前端脚本测试用例
  • 原文地址:https://www.cnblogs.com/wanghao-boke/p/11802088.html
Copyright © 2011-2022 走看看