https://www.acmicpc.net/problem/21608
import sys
from collections import defaultdict
n = int(sys.stdin.readline())
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
mapping = defaultdict(list)
maps = [[0] * n for _ in range(n)]
answer = 0
def check_conditon(y, x, student):
loved, checked = 0, 0
for i in range(4):
cy, cx = y + dy[i], x + dx[i]
if cx < 0 or cx >= n or cy < 0 or cy >= n:
continue
if not maps[cy][cx]:
checked += 1
if maps[cy][cx] in mapping[student]:
loved += 1
return (loved, checked, y, x)
def check_satisfaction(y, x, student):
loved = 0
for i in range(4):
cy, cx = y + dy[i], x + dx[i]
if cx < 0 or cx >= n or cy < 0 or cy >= n:
continue
if maps[cy][cx] in mapping[student]:
loved += 1
if not loved:
return 0
elif loved == 1:
return 1
elif loved == 2:
return 10
elif loved == 3:
return 100
else:
return 1000
for _ in range(n * n):
arr = list(map(int, sys.stdin.readline().split()))
for i in arr[1:]:
mapping[arr[0]].append(i)
for student in list(mapping):
placeInfo = []
for y in range(n):
for x in range(n):
if not maps[y][x]:
placeInfo.append(check_conditon(y, x, student))
placeInfo = sorted(placeInfo, key=lambda x: (-x[0], -x[1], x[2], x[3]))[0]
maps[placeInfo[2]][placeInfo[3]] = student
for y in range(n):
for x in range(n):
answer += check_satisfaction(y, x, maps[y][x])
print(answer)
삼성이 좋아하는 상어 시리즈. 상어 문제하면 떠오르는 악랄한 구현 문제들에 비해 이건 좀 쉬운 편에 속했다.