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;
        }
    }


  • 相关阅读:
    [CXF REST标准实战系列] 二、Spring4.0 整合 CXF3.0,实现测试接口
    [CXF REST标准实战系列] 一、JAXB xml与javaBean的转换
    项目中使用百度地图遇见的问题
    工作体会(第一次工作)
    第一家公司面试
    自我总结(九)---
    J2EE 第二阶段项目(八)
    J2EE 第二阶段项目之编写代码(六)
    J2EE 第二阶段项目之JUnit4进行单元测试(五)
    J2EE 第二阶段项目之编写代码(四)
  • 原文地址:https://www.cnblogs.com/wanghao-boke/p/11802088.html
Copyright © 2011-2022 走看看