<๋ฌธ์ ๋งํฌ>
https://www.acmicpc.net/problem/17070
17070๋ฒ: ํ์ดํ ์ฎ๊ธฐ๊ธฐ 1
์ ํ์ด๊ฐ ์ ์ง์ผ๋ก ์ด์ฌํ๋ค. ์ ์ง์ ํฌ๊ธฐ๋ N×N์ ๊ฒฉ์ํ์ผ๋ก ๋ํ๋ผ ์ ์๊ณ , 1×1ํฌ๊ธฐ์ ์ ์ฌ๊ฐํ ์นธ์ผ๋ก ๋๋์ด์ ธ ์๋ค. ๊ฐ๊ฐ์ ์นธ์ (r, c)๋ก ๋ํ๋ผ ์ ์๋ค. ์ฌ๊ธฐ์ r์ ํ์ ๋ฒํธ, c๋ ์ด์
www.acmicpc.net
<ํ์ด ์ ๋ต>
1. 2์ฐจ์ ๋ฐฐ์ด์์ ๋์ฐฉ์ง๊น์ง์ ๊ฒฝ๋ก์ ๊ฐ์๋ฅผ ์ฐพ๋ ์ ํ์ DP ๋ฌธ์ ์ด์ง๋ง N์ ๋ฒ์๊ฐ ์์ BFS๋ก๋ ํ ์ ์์ต๋๋ค.
2. ์ ๋ DP๋ก ํ์์ผ๋ฉฐ ํ ์ด๋ธ์ ๊ฐ ์ขํ๋ง๋ค ํด๋น ์ขํ๋ฅผ ๋์ ์ผ๋ก ํ๋ ํ์ดํ๊ฐ ๋์ผ ์ ์๋ ๊ฒฝ์ฐ์ ์๋ฅผ ๊ฐ ๋ฐฉํฅ(๊ฐ๋ก, ์ธ๋ก, ๋๊ฐ์ )์ ๋ํด ๊ณ์ฐํ ๋ฐฐ์ด๋ก ์ ์ํ์ต๋๋ค.
3. ํด๋น ์ขํ์ ์ง์ ์ขํ์ ๋ฒฝ ์ฌ๋ถ๋ฅผ ๊ฒ์ฌํ๋ฉด์ ํ ์ด๋ธ์ ์ฑ์ ๋๊ฐ๋ฉด ๋ฉ๋๋ค.
<์ ๋ต ์ฝ๋>
import sys, os, io, atexit
input = lambda: sys.stdin.readline().rstrip('\r\n')
stdout = io.BytesIO()
sys.stdout.write = lambda s: stdout.write(s.encode("ascii"))
atexit.register(lambda: os.write(1, stdout.getvalue()))
# 230404 17070 ํ์ดํ ์ฎ๊ธฐ๊ธฐ
# ์ ๋ต์ฝ๋
N = int(input())
arr = [list(map(int, input().split())) for _ in range(N)]
dp = [[[0, 0, 0] for _ in range(N)] for _ in range(N)] # ๊ฐ๋ก, ์ธ๋ก, ๋๊ฐ์
for r in range(N):
for c in range(1, N):
# ๋ฒฝ
if arr[r][c] == 1:
continue
# ์ฒซ ํ
if r == 0:
# ์ด๊ธฐ์์น
if c == 1:
dp[r][c][0] = 1
continue
# ๊ฐ๋ก
if arr[r][c - 1] == 0:
dp[r][c][0] += dp[r][c - 1][0] + dp[r][c - 1][2]
#๋๊ฐ
if arr[r - 1][c - 1] == 0 and arr[r][c - 1] == 0 and arr[r - 1][c] == 0:
dp[r][c][2] += dp[r - 1][c - 1][0] + dp[r - 1][c - 1][1] + dp[r - 1][c - 1][2]
if 0 <= r - 1:
# ๊ฐ๋ก
if arr[r][c - 1] == 0:
dp[r][c][0] += dp[r][c - 1][0] + dp[r][c - 1][2]
#์ธ๋ก
if arr[r - 1][c] == 0:
dp[r][c][1] += dp[r - 1][c][1] + dp[r - 1][c][2]
#๋๊ฐ
if arr[r - 1][c - 1] == 0 and arr[r][c - 1] == 0 and arr[r - 1][c] == 0:
dp[r][c][2] += dp[r - 1][c - 1][0] + dp[r - 1][c - 1][1] + dp[r - 1][c - 1][2]
print(sum(dp[N - 1][N - 1]))
'โญ Problem_Solving > ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] 1421 ๋๋ฌด๊พผ ์ด๋ค์ (Python/ํ์ด์ฌ) (2) | 2023.05.08 |
---|---|
[๋ฐฑ์ค] 27942 :danceplant: (Python/ํ์ด์ฌ) (1) | 2023.04.12 |
[๋ฐฑ์ค] 2072 ์ค๋ชฉ (Python/ํ์ด์ฌ) (1) | 2022.12.28 |
[๋ฐฑ์ค] 5635 ์์ผ (Python/ํ์ด์ฌ) (2) | 2022.12.18 |
[๋ฐฑ์ค] 2239 ์ค๋์ฟ (Python/ํ์ด์ฌ) (0) | 2022.11.10 |
๋๊ธ