개발자 톡
반례랑 왜 틀렸는지 알고 싶습니다...
- 등록일
- 2023-12-06 22:28:48
- 조회수
- 263
- 작성자
- ga1754
import sys
input = sys.stdin.readline
# N행 M열
# 거리가 같으면 가로 길이부터 세로 순서로
N, M, Q = map(int, input().split())
# id의 0번 인덱스가 IN, id의 1번 인덱스가 OUT
inAndOut = [[0, 0] for _ in range(1001)]
# 대각선으로 일단 위치를 스택에 넣는다.
seats = []
whereSeat = dict()
dr = [-1, 1, 0, 0]
dc = [0, 0, -1, 1]
def isValid(nr, nc):
return 0 <= nr < N and 0 <= nc < M
def findSeat(io, id):
now = inAndOut[id]
if io == "In":
if now[0]:
if now[1]:
return f"{id} already ate lunch."
return f"{id} already seated."
# 자리 찾기
# 찾은 자리 넣기
# 자리가 있으면
if seats:
maxD = 0
canR, canC = 21, 21
newSeats = [[0 for _ in range(M)] for _ in range(N)]
for sr, sc in seats:
newSeats[sr][sc] = -1
for d in range(4):
nr = dr[d] + sr
nc = dc[d] + sc
if isValid(nr, nc):
newSeats[nr][nc] = -1
for sr, sc in seats:
for r in range(N):
for c in range(M):
if newSeats[r][c] != -1:
newSeats[r][c] += (r-sr)**2 + (c-sc)**2
for r in range(N):
for c in range(M):
nowD = newSeats[r][c]
if nowD != -1 and nowD > maxD:
maxD = nowD
canR, canC = r, c
elif nowD == maxD:
if r < canR:
canR, canC = r, c
elif r == canR:
if c < canC:
canR, canC = r, c
if canR == 21:
# 자리 없음
return "There are no more seats."
seats.append((canR, canC))
inAndOut[id][0] = 1
whereSeat[id] = [canR, canC]
return f"{id} gets the seat {canR+1, canC+1}."
else:
seats.append((0, 0))
inAndOut[id][0] = 1
whereSeat[id] = [0, 0]
return f"{id} gets the seat {1, 1}."
else:
if now[0]:
if now[1]:
return f"{id} already left seat."
nowIdSeat = whereSeat[id]
inAndOut[id][1] = 1
seats.remove((nowIdSeat[0], nowIdSeat[1]))
return f"{id} leaves from the seat {nowIdSeat[0]+1, nowIdSeat[1]+1}."
# 아직 점심을 먹지 않음
return f"{id} didn't eat lunch."
for _ in range(Q):
io, number = input().split()
print(findSeat(io, int(number)))
# print(io, number)