Skip to content

Commit 0456ec3

Browse files
committed
2 parents 41219a2 + 6a1d525 commit 0456ec3

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

Hongjoo/백준/내리막길.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import sys
2+
sys.setrecursionlimit(10**7)
3+
4+
m, n = map(int, input().split()) # m: 세로, n: 가로
5+
Map = [list(map(int, sys.stdin.readline().split())) for _ in range(m)]
6+
# DP table 구현
7+
vst = [[-1]*n for _ in range(m)]
8+
9+
def dfs(x, y): # x: row, y: col
10+
11+
dx = [0, 0, -1, 1]
12+
dy = [1, -1, 0, 0]
13+
14+
# 도착 지점에 도달하면 1(한 가지 경우의 수)를 리턴
15+
if x == (m-1) and y ==(n-1):
16+
return 1
17+
18+
# 이미 방문한 적이 있다면 그 위치에서 출발하는 경우의 수를 리턴
19+
if vst[x][y] != -1:
20+
return vst[x][y]
21+
22+
cnt = 0
23+
for i in range(4):
24+
nx = x + dx[i]
25+
ny = y + dy[i]
26+
# 탐색 범위를 벗어나는 경우 pass
27+
if nx<0 or ny<0 or nx>(m-1) or ny>(n-1): continue
28+
# 내리막 일 경우 (현재 길 < 이전 길)
29+
if Map[nx][ny] < Map[x][y]:
30+
cnt += dfs(nx, ny)
31+
32+
vst[x][y] = cnt
33+
return vst[x][y]
34+
35+
print(dfs(0, 0))
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
https://www.acmicpc.net/problem/1520
3+
"""
4+
5+
import sys
6+
input = sys.stdin.readline
7+
#0. 맵 (N,M) 칸 별 탐사 가치 설정
8+
N , M = map(int,input().split()) # y,x
9+
fields =list()
10+
for n in range(N) :
11+
fields.append(list(map(int, input().split())))
12+
13+
# print(fields)
14+
# 2. dp 테이블 초기화
15+
dp = [[0 for _ in range(M)] for k in range(N)]
16+
left2right = [[0 for _ in range(M)] for k in range(N)]
17+
right2left = [[0 for _ in range(M)] for k in range(N)]
18+
#3.탐색 가능 (1)left2 right <-/ (2) right2left <-
19+
20+
# n == 0 경우 : (1) left2right 경로만 존재(누적값)
21+
for m in range(M) :
22+
dp[0][m] = dp[0][m-1] + fields[0][m]
23+
# 2번쨰 행 부터 진행
24+
for n in range(1, N) :
25+
#m==0인 경우 초기화 : top-dowm의 경로만 존재
26+
left2right[n][0] = dp[n-1][0] + fields[n][0]
27+
right2left[n][M-1] = dp[n-1][M-1] + fields[n][M-1]
28+
# 1. 왼쪽 -> 오른쪽 방향으로 탐색 진행
29+
for m in range(1,M) :
30+
# (1)left2right , top-down 방향 탐색 누적 값 구하기
31+
left2right[n][m] = max(dp[n-1][m], left2right[n][m-1])+ fields[n][m]
32+
33+
#2. 오른쪽 -> 왼쪽 방향으로 탐색 진행
34+
for m in range(M-2 , -1 , -1) :
35+
# (2) right2left , top-down 방향 누적값 구하기
36+
right2left[n][m] = max(dp[n-1][m], right2left[n][m+1])+ fields[n][m]
37+
#3. 열 m 탐색 완료시 , 최종 dp 값 계산하기
38+
for m in range(M):
39+
dp[n][m] = max(left2right[n][m] , right2left[n][m])
40+
41+
print(dp[-1][-1])
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
N, M, K = map(int, input().split())
2+
fireballs = []
3+
for _ in range(M):
4+
_r, _c, _m, _s, _d = list(map(int, input().split()))
5+
fireballs.append([_r-1, _c-1, _m, _s, _d])
6+
7+
MAP = [[[] for _ in range(N)] for _ in range(N)]
8+
9+
dx = [-1, -1, 0, 1, 1, 1, 0, -1]
10+
dy = [0, 1, 1, 1, 0, -1, -1, -1]
11+
12+
for _ in range(K):
13+
# 파이어볼 이동
14+
while fireballs:
15+
cr, cc, cm, cs, cd = fireballs.pop(0)
16+
nr = (cr + cs * dx[cd]) % N # 1번-N번 행 연결되어있기 때문
17+
nc = (cc + cs * dy[cd]) % N
18+
MAP[nr][nc].append([cm, cs, cd])
19+
20+
# 2개 이상인지 체크
21+
for r in range(N):
22+
for c in range(N):
23+
# 2개 이상인 경우 -> 4개의 파이어볼로 쪼개기
24+
if len(MAP[r][c]) > 1:
25+
sum_m, sum_s, cnt_odd, cnt_even, cnt = 0, 0, 0, 0, len(MAP[r][c])
26+
while MAP[r][c]:
27+
_m, _s, _d = MAP[r][c].pop(0)
28+
sum_m += _m
29+
sum_s += _s
30+
if _d % 2:
31+
cnt_odd += 1
32+
else:
33+
cnt_even += 1
34+
if cnt_odd == cnt or cnt_even == cnt: # 모두 홀수이거나 모두 짝수인 경우
35+
nd = [0, 2, 4, 6]
36+
else:
37+
nd = [1, 3, 5, 7]
38+
if sum_m//5: # 질량 0이면 소멸
39+
for d in nd:
40+
fireballs.append([r, c, sum_m//5, sum_s//cnt, d])
41+
42+
# 1개인 경우
43+
if len(MAP[r][c]) == 1:
44+
fireballs.append([r, c] + MAP[r][c].pop())
45+
46+
print(sum([f[2] for f in fireballs]))

0 commit comments

Comments
 (0)