몸과 마음이 건전한 SW 개발자

프로그래머스 Lv. 2 행렬 테두리 회전하기 Python 본문

알고리즘

프로그래머스 Lv. 2 행렬 테두리 회전하기 Python

스위태니 2024. 1. 3. 22:18

문제 링크

https://school.programmers.co.kr/tryouts/72048/challenges

정답 코드

def solution(rows, columns, queries):
    newMap = [[0 for _ in range(columns)] for _ in range(rows)]
    
    cnt = 1
    for r in range(rows):
        for c in range(columns):
            newMap[r][c] = cnt
            cnt += 1
    answer = []
    for x1, y1, x2, y2 in queries:
        x1 -= 1
        y1 -= 1
        x2 -= 1
        y2 -= 1
        minV = 10001
        nextV = newMap[x1][y1]
        # 상단
        for ny in range(y1+1, y2+1):
            if minV > nextV:
                minV = nextV
            newMap[x1][ny], nextV = nextV, newMap[x1][ny]

        # 오른쪽
        for nx in range(x1+1, x2+1):
            if minV > nextV:
                minV = nextV
            newMap[nx][y2], nextV = nextV, newMap[nx][y2]

        # 하단
        for ny in range(y2-1, y1-1, -1):
            if minV > nextV:
                minV = nextV
            newMap[x2][ny], nextV = nextV, newMap[x2][ny]

        # 왼쪽
        for nx in range(x2-1, x1-1, -1):
            if minV > nextV:
                minV = nextV
            newMap[nx][y1], nextV = nextV, newMap[nx][y1]
        answer.append(minV)

        
    return answer

풀이 방법

- 단순하게 상단, 오른쪽, 하단, 왼쪽으로 한 바퀴 돌려준다.

- 돌려주는 과정에서 가장 낮은 점수를 찾는다.

느낀점

- 그다지 어려운 문제는 아니므로 실수하지 않게 print로 확인하면서 풀어보면 좋겠다.