문제출처: https://programmers.co.kr/learn/courses/30/lessons/77485
접근법
코드
import copy def solution(rows, columns, queries): answer = [] arr = [ [j for j in range((i-1)*columns+1,(i)*columns+1) ] for i in range(1,rows+1)] for q in queries: x1,y1,x2,y2 = q[0]-1,q[1]-1,q[2]-1,q[3]-1 global min_num min_num = 100000 s = [] def rotate(row,col,count): global min_num if( count and row == x1 and col == y1): return before = s.pop() if s else arr[row][col] min_num = min(min_num,before) if( row == x1 and col+1 <= y2 ): s.append(arr[row][col+1]) arr[row][col+1] = before rotate(row,col+1,count+1) elif( col == y2 and row+1 <= x2 ): s.append(arr[row+1][col]) arr[row+1][col] = before rotate(row+1,col,count+1) elif( row == x2 and col-1 >= y1 ): s.append(arr[row][col-1]) arr[row][col-1] = before rotate(row,col-1,count+1) elif( col == y1 and row-1 >= x1 ): s.append(arr[row-1][col]) arr[row-1][col] = before rotate(row-1,col,count+1) rotate(x1,y1,0) answer.append(min_num) return answer