Link : https://programmers.co.kr/learn/courses/30/lessons/77485
Python
더보기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
def solution(rows, columns, queries):
world = [[tx + (ty * columns) + 1 for tx in range(columns)] for ty in range(rows)]
for i in range(len(queries)):
for j in range(len(queries[0])):
queries[i][j] -= 1
answer = []
for x1, y1, x2, y2 in queries:
# 0 right 1 down 2 left 3 up
result = int(1e9)
pos_y, pos_x = x1, y1
y_bar, x_bar = x2 - x1, y2 - y1
move_weight = [x_bar, y_bar, x_bar, y_bar - 1]
prev_st = world[pos_y][pos_x]
for direction, weight in enumerate(move_weight):
start = True
for move in range(weight):
if direction == 0: # right
pos_x += 1
elif direction == 1: # down
pos_y += 1
elif direction == 2: # left
pos_x -= 1
elif direction == 3: # up
pos_y -= 1
temp = world[pos_y][pos_x]
if not start:
temp = next_st
next_st = world[pos_y][pos_x]
value = temp if start is False else prev_st
world[pos_y][pos_x] = value
if result > value:
result = value
if move == weight - 1:
prev_st = next_st
start = False
world[x1][y1] = prev_st
if result > prev_st:
result = prev_st
answer.append(result)
return answer
|
cs |
FeedBack
- temp와 next_st, start 부분에 대한 정리가 필요함. (움직인 후를 기점으로 미래에 값을 대입하기위해 기억해야 하는 값과 이전의 값을 넣는 부분)
cjw.git@gmail.com
'알고리즘 > 소스코드' 카테고리의 다른 글
PROGRAMMERS 42861: 섬 연결하기 (0) | 2021.07.18 |
---|---|
PROGRAMMERS 42842: 카펫 (0) | 2021.07.18 |
PROGRAMMERS 76502: 괄호 회전하기 (0) | 2021.06.21 |
PROGRAMMERS 12985: 예상 대진표 (0) | 2021.04.05 |
PROGRAMMERS 17684: [3차] 압축 (0) | 2021.04.05 |
댓글