zoukankan      html  css  js  c++  java
  • 图2 Saving James Bond

    题目:https://pintia.cn/problem-sets/1268384564738605056/problems/1281571555116650496

    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 whether or not he can escape.

    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, print in a line "Yes" if James can escape, or "No" if not.

    Sample Input 1:

    14 20
    25 -15
    -25 28
    8 49
    29 15
    -35 -2
    5 28
    27 -29
    -8 -28
    -20 -35
    -25 -20
    -13 29
    -30 15
    -35 40
    12 12
    
     

    Sample Output 1:

    Yes
    
     

    Sample Input 2:

    4 13
    -12 12
    12 12
    -12 -12
    12 -12
    
     

    Sample Output 2:

    No


    题解:https://blog.csdn.net/tuzigg123/article/details/46893747
    代码:

        /*2015.7.15*/
        #include <iostream>
        #include <vector>
        #include <math.h>
        #include <queue>
        #include <fstream>
        using namespace std;
         
        //ifstream fin("case1.txt");
        //#define cin fin
         
        struct point{
            float x;
            float y;
            bool canEscape;
        };
        int main(){
            int N,D;
            cin>>N>>D;
            vector<point> vexs(N);
            vector<vector<bool> > edges(N,vector<bool>(N,false));
         
            for(int i=0;i<N;i++){//鳄鱼序号为0到N-1
                cin>>vexs[i].x>>vexs[i].y;
                if(fabs(vexs[i].x)+D>=50||fabs(vexs[i].y)+D>=50){
                    vexs[i].canEscape=true;  //标记能一步跳到岸边的点
                }else
                    vexs[i].canEscape=false;
            }
            for(int i=0;i<N;i++){
                for(int j=i+1;j<N;j++){
                    float dx=vexs[i].x-vexs[j].x;
                    float dy=vexs[j].y-vexs[j].y;
                    if(dx*dx+dy*dy<=D*D){//标记能够两两连通的点
                        edges[i][j]=true;
                        edges[j][i]=true;
                    }
                }
            }
            //BFS
            queue<int> cur,next;
            vector<int> visited(N,false);
         
            for(int i=0;i<N;i++){
                if(vexs[i].x*vexs[i].x+vexs[i].y*vexs[i].y<=(D+7.5)*(D+7.5)){
                    cur.push(i);//能够从湖中小岛跳到的点入队
                    visited[i]=true;
                }
            }
            
            while(!cur.empty()){
                while(!cur.empty()){
                    int root=cur.front();
                    cur.pop();
                    if(vexs[root].canEscape){
                        cout<<"Yes"<<endl;
                        return 0;
                    }
                    for(int i=0;i<N;i++){
                        if(edges[root][i]&&!visited[i]){
                            next.push(i);
                            visited[i]=true;
                        }
                    }
                }
                swap(cur,next);
            }
            cout<<"No"<<endl;
            return 0;
        }



  • 相关阅读:
    验证码缓存问题完美解决方案
    最近项目是跟框架有关的两个问题
    未与信任 SQL Server 连接相关联
    Get请求
    Post请求
    jQuery操作元素
    Dom对象和jQuery包装集
    XMLHttpRequest对象
    jQuery事件与事件对象
    处理数据集
  • 原文地址:https://www.cnblogs.com/simon-chou/p/13619979.html
Copyright © 2011-2022 走看看