import sys
from collections import deque
dy = [0, 1, 0, -1]
dx = [1, 0, -1, 0]
def solution(rows, columns, queries):
answer = []
graph = [[0] * columns for _ in range(rows)]
number = 1
for i in range(rows):
for j in range(columns):
graph[i][j] = number
number += 1
for query in queries:
left_y, left_x, right_y, right_x = query
left_y, left_x, right_y, right_x = left_y - 1, left_x - 1, right_y - 1, right_x - 1
values = deque()
cur_y, cur_x = left_y, left_x
dir = 0
while len(values) < (right_y - left_y) * 2 + (right_x - left_x) * 2:
values.append(graph[cur_y][cur_x])
ny, nx = cur_y + dy[dir], cur_x + dx[dir]
if nx > right_x or nx < left_x or ny > right_y or ny < left_y:
dir += 1
ny, nx = cur_y + dy[dir], cur_x + dx[dir]
cur_y, cur_x = ny, nx
min_value = sys.maxsize
values.rotate(1)
cur_y, cur_x = left_y, left_x
dir = 0
while values:
cur_val = values.popleft()
if cur_val != graph[cur_y][cur_x]:
min_value = min(min_value, cur_val)
graph[cur_y][cur_x] = cur_val
ny, nx = cur_y + dy[dir], cur_x + dx[dir]
if nx > right_x or nx < left_x or ny > right_y or ny < left_y:
dir += 1
ny, nx = cur_y + dy[dir], cur_x + dx[dir]
cur_y, cur_x = ny, nx
answer.append(min_value)
return answer
print(solution(6, 6, [[2,2,5,4],[3,3,6,6],[5,1,6,3]]) == [8, 10, 25])
print(solution(3, 3, [[1,1,2,2],[1,2,2,3],[2,1,3,2],[2,2,3,3]]) == [1, 1, 5, 3])
print(solution(100, 97, [[1,1,100,97]]) == [1])
지정된 위치의 값들을 한바퀴 돌면서 다 저장하고, 한번 rotate한 다음 다시 그 위치들을 돌면서 깔아주는 것이다.