-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kastenlauf.java
52 lines (44 loc) · 1.17 KB
/
Kastenlauf.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.io.*;
import java.util.*;
import java.lang.Math;
public class Kastenlauf{
public static void main(String[] args){
Scanner ob = new Scanner(System.in);
int testCases = ob.nextInt();
for (int i = 0; i<testCases ;i++ ){
int n = ob.nextInt();
ob.nextLine();
int[] x = new int[n+2];
int[] y = new int[n+2];
int[] parent = new int[n+2];
for(int j = 0 ; j<n+2; j++){
String[] line = ob.nextLine().split(" ");
x[j] = Integer.parseInt(line[0]);
y[j] = Integer.parseInt(line[1]);
parent[j] = j;
}
for(int k = 0; k<n+2; k++){
for(int l = k+1 ; l<n+2 ; l++){
if((Math.abs(x[k]-x[l]) + Math.abs(y[k]-y[l])) <= 1000){
for(int r = 0 ; r< n+2; r++){
int pid = parent[k];
int qid = parent[l];
if(pid == qid){
break;
}
else if(parent[r] == qid){
parent[r] = pid;
}
}
}
}
}
if(parent[0] == parent[n+1]){
System.out.println("happy");
}
else{
System.out.println("sad");
}
}
}
}