[코드트리] 예술성

ewillwin·2024년 4월 11일
0

import sys
from collections import deque
input = sys.stdin.readline

dx = (0, 0, -1, 1)
dy = (-1, 1, 0, 0)

N = int(input())
board = [list(map(int, input().split())) for n in range(N)]
score = []

for _ in range(4):
    ##### 각 Group의 좌표들 구하기
    group_pos = dict()
    visit = [[False]*N for n in range(N)]
    for i in range(N):
        for j in range(N):
            if not visit[i][j]:
                visit[i][j] = True
                queue = deque([]); queue.append((i, j))
                one = set(); one.add((i, j))
                color = board[i][j]
                while queue:
                    x, y = queue.popleft()
                    for d in range(4):
                        nx, ny = x+dx[d], y+dy[d]
                        if 0<=nx<N and 0<=ny<N:
                            if not visit[nx][ny] and board[nx][ny] == color:
                                visit[nx][ny] = True
                                queue.append((nx, ny))
                                one.add((nx, ny))
                group_pos[(i, j)] = one

    ##### Group끼리 인접한 변의 개수 구하기
    adj_cnt = dict()
    for key in group_pos.keys(): adj_cnt[key] = 0
    for key in group_pos.keys():
        one = dict()
        for __ in group_pos.keys(): one[__] = 0
        for x, y in group_pos[key]:
            for d in range(4):
                nx, ny = x+dx[d], y+dy[d]
                if 0<=nx<N and 0<=ny<N and (nx, ny) not in group_pos[key]:
                    for kkey in group_pos.keys():
                        if key != kkey and (nx, ny) in group_pos[kkey]: one[kkey] += 1; break
        adj_cnt[key] = one

    ##### 조화로움 값 구하기
    tmp_score = 0; checked = set()
    for_ = list(group_pos.keys())
    for n in range(len(for_)):
        for m in range(n+1, len(for_)):
            n_len = len(group_pos[for_[n]]); m_len = len(group_pos[for_[m]])
            n_color = board[for_[n][0]][for_[n][1]]; m_color = board[for_[m][0]][for_[m][1]]
            tmp_score += (n_len + m_len) * n_color * m_color * adj_cnt[for_[n]][for_[m]]
    score.append(tmp_score)

    ##### 사각형 회전
    box0 = [[-1]*N for n in range(N)]
    box1 = [[-1]*(N//2) for n in range(N//2)]
    box2 = [[-1]*(N//2) for n in range(N//2)]
    box3 = [[-1]*(N//2) for n in range(N//2)]
    box4 = [[-1]*(N//2) for n in range(N//2)]
    for i in range(N):
        for j in range(N):
            if i == N//2: box0[i][j] = board[i][j]
            else:
                if j == N//2: box0[i][j] = board[i][j]
                elif j < N//2 and i < N//2: box1[i][j] = board[i][j]
                elif j > N//2 and i < N//2: box2[i][j-N//2-1] = board[i][j]
                elif j < N//2 and i > N//2: box3[i-N//2-1][j] = board[i][j]
                elif j > N//2 and i > N//2: box4[i-N//2-1][j-N//2-1] = board[i][j]
    box0 = list(reversed(list(map(list, zip(*box0)))))
    box1 = reversed(box1); box1 = list(map(list, zip(*box1)))
    box2 = reversed(box2); box2 = list(map(list, zip(*box2)))
    box3 = reversed(box3); box3 = list(map(list, zip(*box3)))
    box4 = reversed(box4); box4 = list(map(list, zip(*box4)))

    for i in range(N):
        for j in range(N):
            if i == N//2: board[i][j] = box0[i][j]
            else:
                if j == N//2: board[i][j] = box0[i][j]
                elif j < N//2 and i < N//2: board[i][j] = box1[i][j]
                elif j > N//2 and i < N//2: board[i][j] = box2[i][j-N//2-1]
                elif j < N//2 and i > N//2: board[i][j] = box3[i-N//2-1][j]
                elif j > N//2 and i > N//2: board[i][j] = box4[i-N//2-1][j-N//2-1]

print(sum(score))
profile
💼 Software Engineer @ LG Electronics | 🎓 SungKyunKwan Univ. CSE

0개의 댓글