Skip to content

Commit 0322b41

Browse files
committed
2월 3주차 발제/ 과제 문제 업로드
1 parent 0963f0d commit 0322b41

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
'''
2+
BOJ #1520. 내리막길 (골드3)
3+
https://www.acmicpc.net/problem/1520
4+
유형: Dynamic Programming, Graph Theory, Graph Traversal, Depth-First Search
5+
'''
6+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## 🚀 2월 3주차 (02/17) 스터디 발제 주제: DP
2+
> 발제자: 정수미
3+
4+
### 🗂️ 스터디 자료
5+
- PDF: [바로가기](./Study_BOJ_2169.pdf)
6+
![image](https://github.com/user-attachments/assets/787ac081-bd27-4126-9d2c-3425c9c42127)
7+
8+
9+
### 📖 문제
10+
- [백준 #2169. 로봇 조종하기](https://www.acmicpc.net/problem/2169): DP / 골드2
11+
- 정답 코드: [Study_BOJ_2169_로봇조종하기.py](./Study_BOJ_2169_로봇조종하기.py)
12+
13+
### 💻 과제
14+
- [백준 #1520. 내리막길](https://www.acmicpc.net/problem/1520): DP, 그래프 / 골드3
15+
- 정답 코드: [Assignment_BOJ_1520_내리막길.py](./Assignment_BOJ_1520_내리막길.py)
2.17 MB
Binary file not shown.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'''
2+
BOJ #2169. 로봇 조종하기(골드2)
3+
https://www.acmicpc.net/problem/2169
4+
유형: Dynamic Programming
5+
'''
6+
7+
import sys
8+
input = sys.stdin.readline
9+
10+
# 입력
11+
N, M = map(int, input().split()) # 지도의 크기
12+
grid = [list(map(int, input().split())) for _ in range(N)] # 각 지역의 가치
13+
14+
# DP 테이블
15+
dp = [[0] * M for _ in range(N)]
16+
17+
# 첫 번째 행 초기화 (왼쪽에서 오른쪽으로 누적합)
18+
dp[0][0] = grid[0][0]
19+
for j in range(1, M):
20+
dp[0][j] = dp[0][j-1] + grid[0][j] # (0,1)→(0,2)→(0,3)..
21+
22+
# 두 번째 행부터 (왼쪽에서 오른쪽, 오른쪽에서 왼쪽으로 진행)
23+
for i in range(1, N):
24+
left_to_right = [0] * M
25+
right_to_left = [0] * M
26+
27+
# 왼쪽 → 오른쪽
28+
left_to_right[0] = dp[i-1][0] + grid[i][0] # 첫번째 열은 위쪽에서만 올 수 있음
29+
for j in range(1, M): # 두번째 열부터는 위쪽, 왼쪽에서 오는 경우 중 선택
30+
left_to_right[j] = max(dp[i-1][j], left_to_right[j-1]) + grid[i][j]
31+
32+
# 오른쪽 → 왼쪽
33+
right_to_left[M-1] = dp[i-1][M-1] + grid[i][M-1] # 마지막 열은 위쪽에서만 올 수 있음
34+
for j in range(M-2, -1, -1): # 그 다음 열부터는 위쪽, 오른쪽에서 오는 경우 중 선택
35+
right_to_left[j] = max(dp[i-1][j], right_to_left[j+1]) + grid[i][j]
36+
37+
# 두 개의 배열을 비교해 dp[i][j] 갱신
38+
for j in range(M):
39+
dp[i][j] = max(left_to_right[j], right_to_left[j])
40+
41+
# 정답 출력
42+
print(dp[N-1][M-1]) # 마지막 위치에 저장된 값이 탐사한 지역 가치 합의 최대값

0 commit comments

Comments
 (0)