235. 상어 초등학교

백준




1. Python


import sys
input = sys.stdin.readline
n = int(input())
seat = [[0]*n for _ in range(n)]
like = {}
result = 0
 
dx = [0, 0, -1, 1]
dy = [-1, 1, 0, 0]

for _ in range(n*n):
    data = list(map(int, input().split()))
    like[data[0]] = data[1:]
    
    x = 0
    y = 0
    max_like = -1
    max_empty = -1
    for i in range(n):
        for j in range(n):
            if seat[i][j] == 0: #자리 비었으면
                likecnt = 0
                emptycnt = 0
                for k in range(4): 
                    nx = i + dx[k]
                    ny = j + dy[k]
                    if 0 <= nx < n and 0 <= ny < n:
                        if seat[nx][ny] in data: #좋아하는 사람
                            likecnt += 1
                        if seat[nx][ny] == 0: #비어있는 자리
                            emptycnt += 1
                            
                if max_like < likecnt or (max_like == likecnt and max_empty < emptycnt):
                    x = i
                    y = j
                    max_like = likecnt
                    max_empty = emptycnt
                    
    seat[x][y] = data[0]
    
# 0이면 학생의 만족도는 0, 1이면 1, 2이면 10, 3이면 100, 4이면 1000
for i in range(n):
    for j in range(n):
        cnt = 0
        likelist = like[seat[i][j]]
        for k in range(4):
            nx = i + dx[k]
            ny = j + dy[k]
            if 0 <= nx < n and 0 <= ny < n:
                if seat[nx][ny] in likelist:
                    cnt += 1
        if cnt != 0:
            result += 10 ** (cnt-1)
            
print(result)

좋은 웹페이지 즐겨찾기