zoukankan      html  css  js  c++  java
  • PTA 5-11 Saving James Bond-Hard (30)

    题目:http://pta.patest.cn/pta/test/16/exam/4/question/673

    PTA - Data Structures and Algorithms (English) - 5-11

    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 <iostream>
    #include <fstream>
    #include <cstring>  //memset
    #include <cmath>
    #include <vector>
    #include <queue>
    #include <stack>
    using namespace std;
    const int maxl=10000;
    const double maxd=10005;
     
    int n;
    double pace;
    struct Node //鳄鱼
    {
        int id;
        pair<double, double> site;  //记录鳄鱼坐标 x, y
        double dis;
        bool visit;
        bool operator<(Node a)const  //定义优先级:后面优先队列中在多条相同的最短路径里找第一步最小的
            return a.dis < dis;
    };
    vector<Node>croc;
    vector<int> G[maxl];  //存储图
     
    bool canLand(Node a)  //可以上岸
    {
        if( fabs(a.site.first-50)<=pace || fabs(a.site.first+50)<=pace || 
            fabs(a.site.second-50)<=pace || fabs(a.site.second+50)<=pace )
            return true;
        else
            return false;
    }
     
    int dist[maxl]; //记录在图的哪一层
    int pre[maxl];  //记录路径:存下标i结点前面一个结点的id
    int bfs(Node s)
    {
        memset(dist,-1,sizeof(dist));
        memset(pre,-1,sizeof(pre));
        queue<Node> q;
        q.push(s);
        dist[s.id]=1;       //传进来的s是从原点出发步数为1的结点
        while(!q.empty())   //bfs
        {
            Node tmp=q.front();
            q.pop();
            if(canLand(tmp))
            {
                return tmp.id;  //可以上岸,返回当前结点
            }
            for(unsigned int i=0; i<G[tmp.id].size(); i++)
            {
                int w=G[tmp.id][i];
                if( dist[w]==-1 )  //未被访问过
                {
                    q.push(croc[w]);
                    dist[w]=dist[tmp.id]+1; //步数+1
                    pre[w]=tmp.id;  //以当前结点为下标w,记录这一步的起始结点标号tmp.id
                }
            }
        }
        return 0;  //不能到达,返回 0
    }
     
    int main(int argc, char** argv)
    {
        //ifstream fin("test.txt");
        //fin >> n >> pace;
        cin >> n >> pace;
     
        Node newcroc;
        newcroc.id=newcroc.dis=newcroc.site.first=newcroc.site.second=0;
        croc.push_back(newcroc);    //压入小岛(原点)
        double x, y;
        int id=0;
        while(fin >> x >> y)
        {
            id++;
            newcroc.id=id;
            newcroc.site.first=x;
            newcroc.site.second=y;
            croc.push_back(newcroc);    //压入鳄鱼
        }
     
        //建图
        double tmpdis;
        //first step
        for(int i=1; i<=n; i++)
        {
            tmpdis=sqrt( croc[i].site.first*croc[i].site.first 
                        +croc[i].site.second*croc[i].site.second );
            if(tmpdis<=pace+7.5 && tmpdis>7.5)
            {
                G[0].push_back(i);
                croc[i].dis=tmpdis;
                croc[i].visit=true;
            }
        }
        //not first step
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                tmpdis=sqrt( (croc[j].site.first-croc[i].site.first) * (croc[j].site.first-croc[i].site.first)
                            + (croc[j].site.second-croc[i].site.second) * (croc[j].site.second-croc[i].site.second) );
                if(tmpdis <= pace && i!=j)
                {
                    G[i].push_back(j);
                    if(croc[j].visit==false)
                    croc[j].dis=tmpdis;
                }
             }
        }
     
        //Dijkstra
        if(pace+7.5>=50 || -pace-7.5<=-50)    //特殊情况:若给出的步长能让007从小岛上直接跳到岸上
        {
            cout << "1";
            return 0;
        }
        priority_queue<Node> pq;
        int last=0;   //到岸前最后一个结点(鳄鱼)id
        for(unsigned int i=0; i<G[0].size(); i++)
        {
            pq.push( croc[G[0][i]] );
        }
        int bushu=maxd; //记录最少步数(即最短路)
        stack<int> path;  //根据pre[]和last存最短路的路径
        while(!pq.empty())
        {
            Node v=pq.top();    //结点已按struct中定义的优先级自动排好序
            pq.pop();
            int tmplast=bfs(v);    //当前第一步v对应的last
     
            if(dist[tmplast]<bushu && tmplast>0)    //取步数最少的
            {
                while(!path.empty())
                path.pop();
                bushu=dist[tmplast];
                last=tmplast;
                //回溯
                int tmp=last;
                while(tmp!=-1)
                {
                    path.push(tmp);
                    tmp=pre[tmp];
                }
            }
        }
        if(last==0) //该轮不能到岸
        {
            cout << "0";
            return 0;
        }
        else
        {
            cout << bushu+1 << endl;
            while(!path.empty())
            {
                int pos=path.top();
                path.pop();
                cout << croc[pos].site.first << " " << croc[pos].site.second << endl;
            }
        }
        return 0;
    }
    
    
    
    
  • 相关阅读:
    2020年软件测试需要学什么技术?软件测试人员需要懂代码?软件测试工程师要经常加班吗?
    那些价值数亿的史上最强的【Bug之母】你都知道吗?
    接口自动化测试之-requests模块详解
    linux更新源管理
    Vscode的使用小技巧
    半吊子菜鸟学Web开发 -- PHP学习5-数据库
    半吊子菜鸟学Web开发 -- PHP学习 1-基础语法
    半吊子菜鸟学Web开发 -- PHP学习 4 --异常
    半吊子菜鸟学Web开发 -- PHP学习3-文件
    半吊子菜鸟学Web开发 -- PHP学习2-正则,cookie和session
  • 原文地址:https://www.cnblogs.com/claremore/p/4943519.html
Copyright © 2011-2022 走看看