Saving James Bond - Easy Version java实现

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 NN (\le 100100), the number of crocodiles, andDD, the maximum distance that James could jump. Then NN lines follow, each containing the (x, y)(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.


题目大意:James被困到直径为15的岛上,岛在一个边长100的正方形湖中心,已知James的跳跃距离和湖中鳄鱼的个数以及这个鳄鱼所在的位置坐标,求James能否从岛上通过跳跃到鳄鱼头上最后跳到岸上的方法自救。

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

import java.util.LinkedList;
import java.util.Scanner;
//声明鳄鱼节点,小岛节点为x=0,y=0的节点
class Node
{
    int x;
    int y;

    public Node(int x,int y)
    {
        this.x=x;
        this.y=y;
    }
    //计算当前鳄鱼和制定鳄鱼的距离
    public int getDistant(Node node)
    {
        //如果当前节点是小岛,要考虑小岛的半径
        if (this.x==0&&this.y==0)
            return (int) Math.sqrt((7.5-node.x)*(7.5-node.x)+(7.5-node.y)*(7.5-node.y));
        
        else
            return (int) Math.sqrt((this.x-node.x)*(this.x-node.x)+(this.y-node.y)*(this.y-node.y));
    }
    //计算当前鳄鱼到岸边的最短距离
    public int getDistant()
    {
        int nearest= (50-Math.abs(x))>(50-Math.abs(y))?(50-Math.abs(y)):(50-Math.abs(x));
        return nearest;
    }
}
//声明图类型
class Graph
{
    Node[] arr;
    int distant;
    boolean[] visited;
    public Graph(Node[] arr,int jumpDst)
    {
        this.arr=arr;
        distant=jumpDst;
        visited=new boolean[arr.length];
    }
    public boolean serchBFS()
    {
        //因为不论是否有节点到岸边距离小于跳跃距离,都要从小岛上出发,因此只考虑包含小岛节点的连通集
        LinkedList<Node> l=new LinkedList<>();
        visited[0]=true;
        
        l.add(arr[0]);
        while (l.size()>0)
        {
            Node n=l.remove();
            if (n.getDistant()<=distant)
                return true;
            
            for (int j=0;j<arr.length ;j++ )
            {
                if (n.getDistant(arr[j])<=distant&&visited[j]==false)
                {
                    visited[j]=true;
                    l.add(arr[j]);
                }
            }
        }
        return false;        
    }
}
//测试类
class test
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int num=sc.nextInt();
        int jumpDst=sc.nextInt();
        Node[] arr=new Node[num+1];
        arr[0]=new Node(0,0);
        for (int i=1;i<num+1 ;i++ )
        {
            int x=sc.nextInt();
            int y=sc.nextInt();

            arr[i]=new Node(x,y);

        }
        Graph g=new Graph(arr,jumpDst);
        if (g.serchBFS()==true)
            System.out.print("Yes");
        else
            System.out.print("No");
    }

}

打印结果

 

猜你喜欢

转载自www.cnblogs.com/javaStudy947/p/9076936.html