[코드트리] 고대 문명 유적 탐사

ewillwin·2024년 10월 5일
0

Problem Solving (CodeTree)

목록 보기
13/16

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

K, M = map(int, input().split())
board = [list(map(int, input().split())) for _ in range(5)]
wall = deque(list(map(int, input().split())))

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

def Search(cx, cy, rad, mode): #center x, center y, 1:90/2:180/3:270
    global TestResult

    if mode == True: Tboard = copy.deepcopy(board)
    else: Tboard = board
    Tbox = [[0]*3 for _ in range(3)]
    for x in range(cx-1, cx+1+1):
        for y in range(cy-1, cy+1+1):
            Tbox[x-(cx-1)][y-(cy-1)] = Tboard[x][y]
    for _ in range(rad):
        Tbox = reversed(Tbox); Tbox = list(map(list, zip(*Tbox)))
    for x in range(cx-1, cx+1+1):
        for y in range(cy-1, cy+1+1):
            Tboard[x][y] = Tbox[x-(cx-1)][y-(cy-1)]

    FindResult = FindItem(Tboard)
    if mode == True: heapq.heappush(TestResult, (-len(FindResult), rad, cy, cx))
    return FindResult
    
def FindItem(Tboard):
    visit = [[False]*5 for _ in range(5)]
    result = [] #없어질 좌표
    for i in range(5):
        for j in range(5):
            if not visit[i][j]:
                num = Tboard[i][j]
                local_result = []
                queue = deque([]); queue.append((i, j))
                visit[i][j] = True
                while queue:
                    x, y = queue.popleft()
                    local_result.append((x, y))
                    for d in range(4):
                        nx, ny = x+dx[d], y+dy[d]
                        if 0<=nx<5 and 0<=ny<5:
                            if not visit[nx][ny] and Tboard[nx][ny] == num:
                                visit[nx][ny] = True
                                queue.append((nx, ny))
                if len(local_result) >= 3: result.extend(local_result)
    return result


for k in range(K):
    TurnResult = 0
    ##### [1] 탐사 진행
    TestResult = [] #-값, 각도, 열, 행
    for i in range(1, 3+1):
        for j in range(1, 3+1):
            Search(i, j, 1, True)
            Search(i, j, 2, True)
            Search(i, j, 3, True)

    rad, cy, cx = TestResult[0][1], TestResult[0][2], TestResult[0][3]
    Result = Search(cx, cy, rad, False)
    if len(Result) == 0: break
    else:
        for x, y in Result: board[x][y] = 0
        TurnResult += len(Result)

    ##### [2] 유물 획득
    while True:
        ## 조각 채워짐
        for j in range(5):
            for i in range(4, -1, -1):
                if board[i][j] == 0: board[i][j] = wall.popleft()

        ## 유물 획득
        Result = FindItem(board)
        if len(Result) != 0:
            for x, y in Result: board[x][y] = 0
            TurnResult += len(Result)
        else: break

    print(TurnResult, end=" ")

개발자로 살아남는 법 1: 내 코드는 아무도 못알아보게 짠다

profile
💼 Software Engineer @ LG Electronics | 🎓 SungKyunKwan Univ. CSE

0개의 댓글