본문 바로가기
ps

[백준] - 14719 빗물

by FAPER 2025. 4. 22.

https://www.acmicpc.net/problem/14719

 

생각을 코드로 구현하면 되는 문제이다. 

 

일단 1이 벽이고 0이 빈칸이라고 했을 때, 첫번째 1이 생겨서 벽이 생기면 그 이후로는 전부 물이 고일 가능성이 존재한다.

그리고 다음 1이 나오면 연속된 0을 ans에 더해주면 된다.

H,W = map(int, input().split())
board = [[0]*W for _ in range(H)]
l = list(map(int, input().split()))
for i in range(len(l)):
    for j in range(l[i]):
        board[j][i] = 1

ans = 0

for i in range(H):
    wall_cnt = 0
    water_tmp = 0
    for j in range(W):
        if board[i][j] == 1:
            if wall_cnt > 0 and water_tmp > 0:
                ans += water_tmp
            water_tmp = 0
            wall_cnt += 1
        elif board[i][j] == 0 and wall_cnt > 0:
            water_tmp += 1
print(ans)

'ps' 카테고리의 다른 글

[백준] 1034 램프  (0) 2025.04.27
[백준] 1334 다음 팰린드롬 수  (0) 2025.04.20
[백준] 10427 빚  (0) 2025.03.16
알고리즘문제해결전략 - 완전 탐색  (0) 2025.02.06
알고리즘문제해결전략 - 알고리즘 정당성의 증명  (0) 2025.02.04