ps | |
---|---|
링크 | acmicpc.net/… |
출처 | BOJ |
문제 번호 | 14390 |
문제명 | 타일 놓기 |
레벨 | 플래티넘 1 |
분류 |
DP |
시간복잡도 | O(n*m*2^m) |
인풋사이즈 | n<=10, m<=10 |
사용한 언어 | Python |
제출기록 | 30864KB / 164ms |
최고기록 | 164ms |
해결날짜 | 2022/03/23 |
"""Solution code for "BOJ 14390. 타일 놓기".
- Problem link: https://www.acmicpc.net/problem/14390
- Solution link: http://www.teferi.net/ps/problems/boj/14390
Tags: [DP]
"""
INF = float('inf')
PILLAR = '#'
EMPTY = '.'
def main():
N, M = [int(x) for x in input().split()]
board = [input() for _ in range(N)]
state_size = 1 << M
mask = state_size - 1
first_bit = 1 << (M - 1)
dp_cur = [0] * state_size
for r, row in enumerate(board):
for c, val in enumerate(row):
dp_prev, dp_cur = dp_cur, [INF] * state_size
for state_prev, dp_val in enumerate(dp_prev):
state_cur_v = (state_prev << 1) & mask
state_cur_h = state_cur_v + 1
if val == PILLAR:
dp_cur[state_cur_v] = min(dp_cur[state_cur_v], dp_val)
dp_cur[state_cur_h] = min(dp_cur[state_cur_h], dp_val)
else:
is_connected_v = (r > 0 and board[r - 1][c] == EMPTY and
(state_prev & first_bit) == 0)
is_connected_h = (c > 0 and board[r][c - 1] == EMPTY and
(state_prev & 1) == 1)
dp_cur[state_cur_v] = min(
dp_cur[state_cur_v],
dp_val if is_connected_v else dp_val + 1)
dp_cur[state_cur_h] = min(
dp_cur[state_cur_h],
dp_val if is_connected_h else dp_val + 1)
print(min(dp_cur))
if __name__ == '__main__':
main()