06-图2 Saving James Bond - Easy Version

//06-图2 Saving James Bond - Easy Version
import java.util.Scanner;
import java.lang.Math;
import java.util.ArrayList;


class node{
    int data;
    node next;
}

class location{
    int x;
    int y;
}

public class Main {
    static boolean[] visited = new boolean[100];
    static void read(Scanner s, location[] locations){
        for(int i = 0; i < locations.length; i ++){
            locations[i] = new location();
            locations[i].x = s.nextInt();
            locations[i].y = s.nextInt();
        }
    }

    static double distance(int x1, int y1, int x2, int y2){
        return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
    }

    static void nodeadd(node n, int x){
        node newnode = new node();
        newnode.data = x;
        newnode.next = n.next;
        n.next = newnode;
    }

    static void graphadd(node[] nodes, int i, int j){
        nodeadd(nodes[i], j);
        nodeadd(nodes[j], i);
    }

    static void bulid(location[] locations, node[] nodes, int D){
        for(int i = 0; i < locations.length - 1; i ++){
            for(int j = i + 1; j < locations.length; j ++){
                if(distance(locations[i].x, locations[i].y, locations[j].x, locations[j].y) <= D){
                    graphadd(nodes, i, j);
                }
            }
        }
    }

    static void start(location[] locations, int D, ArrayList<Integer> arr){
        for(int i = 0; i < locations.length; i ++){
            if(distance(locations[i].x, locations[i].y, 0, 0) <= D + 15) arr.add(i);
        }
    }

    static void dfs(node[] nodes, node v, ArrayList<Integer> path){
        visited[v.data] = true;
        for(node p = v.next; p != null; p = p.next){
            if(!visited[p.data]) {
                path.add(p.data);
                dfs(nodes, nodes[p.data], path);
            }
        }
    }

    static void judge(ArrayList<Integer>[] paths, location[] locations, int D){
        String result = "No";
        for(int i = 0; i < paths.length; i ++){
            for(int j = 0; j < paths[i].size(); j ++){
                if(Math.abs(locations[paths[i].get(j)].x) >= (50 - D) || Math.abs(locations[paths[i].get(j)].y) >= (50 - D)){
                    result = "Yes";
                    break;
                }
            }
        }
        System.out.println(result);
    }

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        ArrayList<Integer> arr = new ArrayList<>();
        int N, D;
        N = s.nextInt();
        D = s.nextInt();
        node[] nodes = new node[N];
        for(int i = 0; i < N; i ++){
            nodes[i] = new node();
            nodes[i].data = i;
        }
        location[] locations = new location[N];
        read(s, locations);
        bulid(locations, nodes, D);
        start(locations, D, arr);
        ArrayList<Integer>[] paths = new ArrayList[arr.size()];
        for(int i = 0; i < arr.size(); i ++){
            paths[i] = new ArrayList<>();
            paths[i].add(nodes[arr.get(i)].data);
            dfs(nodes, nodes[arr.get(i)], paths[i]);
        }
        judge(paths, locations, D);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值