Softeer [21년 재직자 대회 예선] 좌석 관리 (난이도 3)

Yibangwon·2022년 8월 20일
0

알고리즘 문제풀이

목록 보기
51/60
post-thumbnail


정답 코드

import sys
N, M, Q = map(int, sys.stdin.readline().split())
ID = [0 for i in range(10001)]
table = [[0 for i in range(M + 2)] for j in range(N + 2)]

def nearest(x, y):
    minD = 1000
    for i in range(1, N + 1):
        for j in range(1, M + 1):
            if table[i][j]:
                d = (x - i) ** 2 + (y - j) ** 2
                if d < minD:
                    minD = d
    return minD

def assign(pid):
    maxD = 0
    for i in range(1, N + 1):
        for j in range(1, M + 1):
            if table[i][j] == 0 and table[i - 1][j] == 0 and table[i + 1][j] == 0\
            and table[i][j - 1] == 0 and table[i][j + 1] == 0:
                d = nearest(i, j)
                if d > maxD:
                    maxD = d
                    ID[pid] = [i, j]
    if maxD == 0:
        return False
    else:
        table[ID[pid][0]][ID[pid][1]] = 1
        return True

for i in range(Q):
    a, pid = sys.stdin.readline().split()
    pid = int(pid)
    if a == 'In':
        if ID[pid] == 0: #not in
            if assign(pid):
                print(f'{pid} gets the seat ({ID[pid][0]}, {ID[pid][1]}).')
            else:
                print('There are no more seats.')
        elif ID[pid] == 1: #already in
            print(f'{pid} already ate lunch.')
        else:
            print(f'{pid} already seated.')
    else: #out
        if ID[pid] == 0: #not in
            print(f"{pid} didn't eat lunch.")
        elif ID[pid] == 1:
            print(f'{pid} already left seat.')
        else:
            print(f'{pid} leaves from the seat ({ID[pid][0]}, {ID[pid][1]}).')
            table[ID[pid][0]][ID[pid][1]] = 0
            ID[pid] = 1

후기

다시 한 번 풀어봐야겠다. assign 구현에서 오래 걸렸다.

profile
I Don’t Hope. Just Do.

0개의 댓글