|
| 1 | +package day1011; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | +import java.io.*; |
| 5 | + |
| 6 | +public class JY_9205 { |
| 7 | + |
| 8 | + static int N; |
| 9 | + static int[] dx = {0, 0, 1, -1}; |
| 10 | + static int[] dy = {1, -1, 0, 0}; |
| 11 | + static Pos[] stores; |
| 12 | + static boolean canGo; |
| 13 | + static Pos home, festival; |
| 14 | + |
| 15 | + static class Pos { |
| 16 | + int x, y, beer; |
| 17 | + |
| 18 | + public Pos(int x, int y, int beer) { |
| 19 | + super(); |
| 20 | + this.x = x; |
| 21 | + this.y = y; |
| 22 | + this.beer = beer; |
| 23 | + } |
| 24 | + |
| 25 | + @Override |
| 26 | + public String toString() { |
| 27 | + return "Pos [x=" + x + ", y=" + y + ", beer=" + beer + "]"; |
| 28 | + } |
| 29 | + |
| 30 | + } |
| 31 | + |
| 32 | + public static void main(String[] args) throws IOException{ |
| 33 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 34 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 35 | + |
| 36 | + int T = Integer.parseInt(st.nextToken()); |
| 37 | + |
| 38 | + for(int t=0; t<T; t++) { |
| 39 | + st = new StringTokenizer(br.readLine()); |
| 40 | + N = Integer.parseInt(st.nextToken()); |
| 41 | + st = new StringTokenizer(br.readLine()); |
| 42 | + int hx = Integer.parseInt(st.nextToken()); |
| 43 | + int hy = Integer.parseInt(st.nextToken()); |
| 44 | + home = new Pos(hx, hy, 20); |
| 45 | + |
| 46 | + stores = new Pos[N]; |
| 47 | + for(int i=0; i<N; i++) { |
| 48 | + st = new StringTokenizer(br.readLine()); |
| 49 | + int sx = Integer.parseInt(st.nextToken()); |
| 50 | + int sy = Integer.parseInt(st.nextToken()); |
| 51 | + stores[i] = new Pos(sx, sy, 0); |
| 52 | + } |
| 53 | + |
| 54 | + st = new StringTokenizer(br.readLine()); |
| 55 | + int fx = Integer.parseInt(st.nextToken()); |
| 56 | + int fy = Integer.parseInt(st.nextToken()); |
| 57 | + festival = new Pos(fx, fy, 0); |
| 58 | + |
| 59 | + // BFS 탐색 |
| 60 | + canGo = false; |
| 61 | + bfs(); |
| 62 | + |
| 63 | + if(canGo) System.out.println("happy"); |
| 64 | + else System.out.println("sad"); |
| 65 | + } |
| 66 | + |
| 67 | + } |
| 68 | + public static void bfs() { |
| 69 | + Queue<Pos> q = new LinkedList<>(); |
| 70 | + boolean[] visited = new boolean[N]; |
| 71 | + q.add(home); |
| 72 | + |
| 73 | + while(!q.isEmpty()) { |
| 74 | + Pos now = q.poll(); |
| 75 | + |
| 76 | + // 현재 위치에서 페스티벌로 갈 수 있다면 break |
| 77 | + if(isGo(now, festival)) { |
| 78 | + canGo = true; |
| 79 | + break; |
| 80 | + } |
| 81 | + // 아직 못간다면, 갈 수 있는 편의점 탐색 |
| 82 | + for(int i=0; i<N; i++) { |
| 83 | + if(!visited[i]) { |
| 84 | + Pos nextStore = stores[i]; |
| 85 | + if(isGo(now, nextStore)) { |
| 86 | + visited[i] = true; |
| 87 | + // 맥주 장전 |
| 88 | + nextStore.beer = 20; |
| 89 | + q.add(nextStore); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + } |
| 96 | + public static boolean isGo(Pos from, Pos to) { |
| 97 | + // 현재 맥주로 갈 수 있는 최대거리 |
| 98 | + int maxDist = from.beer * 50; |
| 99 | + // 맨하탄 거리 |
| 100 | + int distance = Math.abs(from.x-to.x)+Math.abs(from.y-to.y); |
| 101 | + // 남은 거리가 더 많으면 불가능 |
| 102 | + if(distance > maxDist) return false; |
| 103 | + return true; |
| 104 | + |
| 105 | + } |
| 106 | + |
| 107 | +} |
0 commit comments