슈콩

[BOJ] 9205 맥주 마시면서 걸어가기 본문

Algorithms/Baekjoon

[BOJ] 9205 맥주 마시면서 걸어가기

shukong 2026. 2. 10. 21:59

[문제]

https://www.acmicpc.net/problem/9205

 

 

[소스 코드]

import java.io.*;
import java.util.*;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		int t = Integer.parseInt(br.readLine());
		while(t-->0) {
			int n = Integer.parseInt(br.readLine());
			st = new StringTokenizer(br.readLine());
			int houseR = Integer.parseInt(st.nextToken());
			int houseC = Integer.parseInt(st.nextToken());
			int[][] point = new int[n][2];
			for(int i=0;i<n;i++) {
				st = new StringTokenizer(br.readLine());
				point[i][0] = Integer.parseInt(st.nextToken());
				point[i][1] = Integer.parseInt(st.nextToken());
			}
			st = new StringTokenizer(br.readLine());
			int endR = Integer.parseInt(st.nextToken());
			int endC = Integer.parseInt(st.nextToken());
			
			boolean can = false;
			boolean[] visit = new boolean[n];
			Queue<int[]> q = new LinkedList<>();
			q.offer(new int[] {houseR,houseC});
			while(!q.isEmpty()) {
				int[] curr = q.poll();
				int check = Math.abs(curr[0]-endR) + Math.abs(curr[1]-endC);
				if(check<=1000) {
					can = true; 
				}
				for(int i=0;i<n;i++) {
					if(!visit[i]) {
						int diff = Math.abs(curr[0] - point[i][0]) + Math.abs(curr[1] - point[i][1]);
						if(diff<=1000) {
							visit[i] = true;
							q.offer(new int[] {point[i][0],point[i][1]});
						}
					}
				}
			}
			if(can) {
				System.out.println("happy");
			}else {
				System.out.println("sad");
			}
		}
		
	}
}

'Algorithms > Baekjoon' 카테고리의 다른 글

[BOJ] 14503 로봇 청소기  (0) 2026.02.22
[BOJ] 2573 빙산  (0) 2026.02.09
[BOJ] 2468 안전 영역  (0) 2026.02.09
[BOJ] 5014 스타트링크  (0) 2026.02.02
[BOJ] 1697 숨바꼭질  (0) 2026.01.28