728x90
https://www.acmicpc.net/problem/21608
import sys
from collections import defaultdict
input = sys.stdin.readline
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
sum = 0
N = int(input())
seat = [[0] * (N + 1) for _ in range(N + 1)]
student = defaultdict(list)
for _ in range(N ** 2):
info = list(map(int, input().split()))
student_num = info[0]
student_like = info[1:]
student[student_num] = student_like
#앉을 수 있는 자리 후보군
able = []
for x in range(1, N + 1):
for y in range(1, N + 1):
if seat[x][y] == 0:
like = 0
empty = 0
for k in range(4):
nx = x + dx[k]
ny = y + dy[k]
if 1 <= nx < N + 1 and 1 <= ny < N + 1:
if seat[nx][ny] == 0:
empty += 1
if seat[nx][ny] in student[student_num]:
like += 1
able.append((like, empty, x, y))
able = sorted(able, key = lambda x : (-x[0], -x[1], x[2], x[3]))
seat[able[0][2]][able[0][3]] = student_num
for i in range(1, N + 1):
for j in range(1, N + 1):
count = 0
for k in range(4):
nx = i + dx[k]
ny = j + dy[k]
if 1 <= nx < N + 1 and 1 <= ny < N + 1:
if seat[nx][ny] in student[seat[i][j]]:
count += 1
if count != 0:
sum += 10 ** (count - 1)
print(sum)
728x90
'Study > Coding Test' 카테고리의 다른 글
[프로그래머스] 구명보트 Python (0) | 2023.09.28 |
---|---|
[백준] 1431 - 시리얼 번호 Python (0) | 2023.08.14 |
[백준] 1967 - 트리의 지름 Python (0) | 2023.08.09 |
[프로그래머스] 가장 큰 수 Python (0) | 2023.08.09 |
[프로그래머스] 등굣길 Python (0) | 2023.08.09 |