[백준 9205번] 맥주 마시면서 걸어가기

박형진·2023년 1월 31일
0

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

1. 코드

import sys
from collections import deque, defaultdict

t = int(sys.stdin.readline())

for _ in range(t):
    flag = False
    n = int(sys.stdin.readline())
    home = tuple(map(int, sys.stdin.readline().rstrip().split()))
    visited = defaultdict(bool)
    c = []
    for _ in range(n):
        x, y = map(int, sys.stdin.readline().rstrip().split())
        c.append((x, y))
        visited[(x, y)] = False
    px, py = tuple(map(int, sys.stdin.readline().rstrip().split()))

    q = deque()
    q.append(home)
    while q:
        x, y = q.popleft()

        if abs(px - x) + abs(py - y) <= 1000:
            flag = True
            break

        for cx, cy in c:
            if not visited[(cx, cy)]:
                dis = abs(cx - x) + abs(cy - y)
                if dis <= 1000:
                    visited[(cx, cy)] = True
                    q.append((cx, cy))

    if flag:
        print('happy')
    else:
        print('sad')
profile
안녕하세요!

0개의 댓글