행렬 테두리 회전하기
코딩테스트 연습 > 2021 Dev-Matching: 웹 백엔드 개발자(상반기) > 행렬 테두리 회전하기
https://programmers.co.kr/learn/courses/30/lessons/77485#qna
Input value = rows, columns, queries
Process =
왼쪽
return 움직인 원소들의 최소값
아래쪽
return 움직인 원소들의 최소값(갱신)
오른쪽
return 움직인 원소들의 최소값(갱신)
위쪽
return 움직인 원소들의 최소값(갱신)
list(min(시작값, 갱신된 최소값))
Output value= list(min)
answer = []
array = [[0 for col in range(columns)] for row in range(rows)]
t = 1
for row in range(rows):
for col in range(columns):
array[row][col] = t
t += 1
for x1,y1,x2,y2 in queries:
tmp = array[x1-1][y1-1]
mini = tmp
for k in range(x1-1,x2-1):
test = array[k+1][y1-1]
array[k][y1-1] = test
mini = min(mini, test)
for k in range(y1-1,y2-1):
test = array[x2-1][k+1]
array[x2-1][k] = test
mini = min(mini, test)
for k in range(x2-1,x1-1,-1):
test = array[k-1][y2-1]
array[k][y2-1] = test
mini = min(mini, test)
for k in range(y2-1,y1-1,-1):
test = array[x1-1][k-1]
array[x1-1][k] = test
mini = min(mini, test)
array[x1-1][y1] = tmp
answer.append(mini)
def solution(rows, columns, queries):
answer = []
array = [[0 for col in range(columns)] for row in range(rows)]
t = 1
for row in range(rows):
for col in range(columns):
array[row][col] = t
t += 1
for x1,y1,x2,y2 in queries:
tmp = array[x1-1][y1-1]
mini = tmp
for k in range(x1-1,x2-1):
test = array[k+1][y1-1]
array[k][y1-1] = test
mini = min(mini, test)
for k in range(y1-1,y2-1):
test = array[x2-1][k+1]
array[x2-1][k] = test
mini = min(mini, test)
for k in range(x2-1,x1-1,-1):
test = array[k-1][y2-1]
array[k][y2-1] = test
mini = min(mini, test)
for k in range(y2-1,y1-1,-1):
test = array[x1-1][k-1]
array[x1-1][k] = test
mini = min(mini, test)
array[x1-1][y1] = tmp
answer.append(mini)
return answer