06-图2 Saving James Bond - Easy Version

tech2023-02-12  104

//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)