[20250829] BOJ / P5 / 바리스타의 힘 / 권혁준 #772
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
🧷 문제 링크
https://www.acmicpc.net/problem/24439
🧭 풀이 시간
20분
👀 체감 난이도
✏️ 문제 설명
N*M 크기의 격자판에서 (1,1) -> (N,M)으로 이동하려 한다.
능력을 쓰면 상하좌우 중 한 방향을 골라서 벽을 모두 없애버릴 수 있다.
능력을 최대 한 번 사용할 때, 최소 이동 횟수를 구해보자.
🔍 풀이 방법
어떤 칸 (i,j)에서 왼쪽 혹은 위쪽으로 능력을 썼을 때 절대 이득을 볼 수 없다.
(i,j)로 오면서 지났던 칸 중에서 오른쪽 혹은 아래쪽으로 능력을 쓰는 게 더 이득이기 때문임
(1,1)에서 BFS를 돌렸을 때의 최단 거리 배열을 A,
(N,M)에서 BFS를 돌렸을 떄의 최단 거리 배열을 B로 둔다.
어떤 칸 (i,j)에서 오른쪽으로 능력을 써서 (i,y)까지 이동했다면, 거리는 A[i][j] + (y-j) + B[i][y]가 된다.
이때, i와 j는 고정된 값이기 때문에 B[i][y] + y가 가장 작은 곳으로 이동하는 게 제일 이득이다.
-> 누적 합 비슷한 느낌으로 각 행 별로 어디로 가야하는지 누적 최소를 이용해 미리 구할 수 있음
아래쪽으로 능력을 쓰는 경우도 위에서 한 거랑 같은 논리로 구해준 다음, 모든 칸에서 능력을 한 번씩 썼을 때 언제 최소가 되는지 구해줬다.
⏳ 회고
easy