Skip to content

Conversation

@Mingguriguri
Copy link
Collaborator

🌱WIL

이번 한 주의 소감을 작성해주세요!

  • 이번 주도 정신없어서 많이 풀지는 못했다..! 다음 주도 그럴 것 같다.. 3월부터 다시 시작할 예정이다.
  • DP 문제와 DFS 문제가 합쳐진 문제를 풀면서 이런 식으로 내가 알던 알고리즘 개념이 묶일 수 있다는 것을 알게 되었다.

🚀주간 목표 문제 수: 5개

푼 문제


백준 #2169. 로봇 조종하기: DP / 골드2

정리한 링크: (바로가기)

🚩플로우 (선택)

코드를 풀이할 때 적었던 플로우가 있나요?

🚩제출한 코드

import sys
input = sys.stdin.readline

# 입력
N, M = map(int, input().split())
nums = []
for _ in range(N):
    nums.append(list(map(int, input().split())))

dp = [[0] * M for _ in range(N)]

# 첫 줄
dp[0][0] = nums[0][0]
for j in range(1, M):
    dp[0][j] = dp[0][j - 1] + nums[0][j]

# 왼쪽 누적합 업데이트 -> 오른쪽 누적합 업데이트, 이후 둘 중 더 큰 값으로 업데이트
for i in range(1, N):

    left_prefix_sum = [0] * M  # 왼쪽 누적합
    right_prefix_sum = [0] * M  # 오른쪽 누적합

    # 왼쪽 누적합
    left_prefix_sum[0] = dp[i-1][0] + nums[i][0]
    for j in range(1, M):
        left_prefix_sum[j] = max(dp[i-1][j], left_prefix_sum[j-1]) + nums[i][j]

    # 오른쪽 누적합
    right_prefix_sum[M-1] = dp[i-1][M-1] + nums[i][M-1]
    for j in range(M-2, -1, -1):
        right_prefix_sum[j] = max(dp[i-1][j], right_prefix_sum[j+1]) + nums[i][j]

    # DP에 저장
    for j in range(M):
        dp[i][j] = max(left_prefix_sum[j], right_prefix_sum[j])

# 정답 출력
print(dp[N-1][M-1])

💡TIL

배운 점이 있다면 입력해주세요


백준 #1520. 내리막길: DP / 골드3

정리한 링크: (바로가기)

🚩플로우 (선택)

코드를 풀이할 때 적었던 플로우가 있나요?

🚩제출한 코드

import sys

sys.setrecursionlimit(10 ** 8)
input = sys.stdin.readline


def dfs(sx, sy):
    # 도착 지점에 도달하면 1(한 가지 경우의 수)를 리턴
    if sx == m - 1 and sy == n - 1:
        return 1

    # 이미 방문한 적이 있다면 그 위치에서 출발하는 경우의 수를 리턴
    if dp[sx][sy] != -1:
        return dp[sx][sy]

    ways = 0
    for i in range(4):
        nx, ny = sx + dx[i], sy + dy[i]
        if 0 <= nx < m and 0 <= ny < n and graph[sx][sy] > graph[nx][ny]:
            ways += dfs(nx, ny)

    dp[sx][sy] = ways
    return dp[sx][sy]


m, n = map(int, input().split())
graph = [list(map(int, input().split())) for _ in range(m)]
dp = [[-1] * n for _ in range(m)]
dx, dy = [1, -1, 0, 0], [0, 0, 1, -1]

print(dfs(0, 0))

💡TIL

배운 점이 있다면 입력해주세요

@github-actions
Copy link

github-actions bot commented Apr 6, 2025

2025-04 챌린지 진행 상황

사용자 챌린지 유형 문제 수 달성 여부
Mingguriguri 그래프 0
Mingguriguri DP 2

@Mingguriguri Mingguriguri merged commit 851a92f into main Apr 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants