알고리즘/programmers
[ programmers ] 행렬 테두리 회전하기 77485번 ( python )
Yanoo
2021. 9. 8. 22:32
728x90
반응형
문제
프로그래머스 77485번 행렬 테두리 회전하기 풀이 ( 파이썬 )
https://programmers.co.kr/learn/courses/30/lessons/77485
코딩테스트 연습 - 행렬 테두리 회전하기
6 6 [[2,2,5,4],[3,3,6,6],[5,1,6,3]] [8, 10, 25] 3 3 [[1,1,2,2],[1,2,2,3],[2,1,3,2],[2,2,3,3]] [1, 1, 5, 3]
programmers.co.kr
풀이
회전한다고 했을 때 동그라미 친 값들은 따로 저장하여(temp) 이 문제를 해결했다.
def solution(rows, columns, queries):
answer = []
graph = [list(range(1 + (i * columns), 1 + (i * columns) + columns)) for i in range(rows)]
for query in queries:
x1, y1, x2, y2 = query
temp = graph[x1 - 1][y2 - 1]
temp1 = graph[x2 - 1][y2 - 1]
temp2 = graph[x2 - 1][y1 - 1]
ans = min(temp, temp1, temp2)
for i in range(y2 - 1, y1 - 1, -1):
graph[x1 - 1][i] = graph[x1 - 1][i - 1]
ans = min(ans, graph[x1 - 1][i])
for i in range(x2 - 1, x1, - 1):
graph[i][y2 - 1] = graph[i - 1][y2 - 1]
ans = min(ans, graph[i][y2 - 1])
for i in range(y1 - 1, y2 - 1):
graph[x2 - 1][i] = graph[x2 - 1][i + 1]
ans = min(ans, graph[x2 - 1][i])
for i in range(x1 - 1 , x2 - 2):
graph[i][y1 - 1] = graph[i + 1][y1 - 1]
ans = min(ans, graph[i][y1 - 1])
graph[x1][y2 - 1] = temp
graph[x2 - 1][y2 - 2] = temp1
graph[x2 - 2][y1 - 1] = temp2
answer.append(ans)
print(answer)
return answer
728x90
반응형