https://www.acmicpc.net/problem/17070
ํ์ด์ฌ์ด๋ผ ์ต๊น๋นํจ. ๋์ด๋๋ ๋ฎ์ง๋ง ์๊ฐ ์ด๊ณผ๋ก ๊ณ ์ํ๋ค.
๋งจ ์ฒ์ bfs๋ก ํ์์๋ ์๊ฐ ์ด๊ณผ๊ฐ ์ฑ์ 70%์์ ์๊พธ ๊ฑธ๋ฆฌ๊ธธ๋ ๋ฐฉ๋ฒ์ด ํ๋ฆฐ ์ค ์์๋ค.
์๋ง ํ์ด์ฌ์ด ์๋์์ผ๋ฉด ๋ง์์ ๊ฑฐ ๊ฐ๋ค.
from collections import deque
import sys
count = 0
N = int(input())
home = []
for _ in range(N):
home.append([int(x) for x in sys.stdin.readline().rstrip().split()])
def bfs():
global count
deq = deque()
deq.append([0,1,0]) # 0 ๊ฐ๋ก 1 ์ธ๋ก 2 ๋๊ฐ์
while deq:
x, y, state = deq.popleft()
if x == N - 1 and y == N - 1:
count += 1
continue
if state == 0:
if y == N - 1:
continue
if 0 <= x < N and 0 <= y + 1 < N and home[x][y + 1] == 0:
deq.append([x, y + 1, 0])
if 0 <= x + 1 < N and 0 <= y + 1 < N and home[x][y + 1] == 0 and home[x + 1][y] == 0 and home[x + 1][y + 1] == 0:
deq.append([x + 1, y + 1, 2])
elif state == 1:
if x == N - 1:
continue
if 0 <= x + 1 < N and 0 <= y < N and home[x + 1][y] == 0:
deq.append([x + 1, y, 1])
if 0 <= x + 1 < N and 0 <= y + 1 < N and home[x][y + 1] == 0 and home[x + 1][y] == 0 and home[x + 1][y + 1] == 0:
deq.append([x + 1, y + 1, 2])
elif state == 2:
if 0 <= x < N and 0 <= y + 1 < N and home[x][y + 1] == 0:
deq.append([x, y + 1, 0])
if 0 <= x + 1 < N and 0 <= y < N and home[x + 1][y] == 0:
deq.append([x + 1, y, 1])
if 0 <= x + 1 < N and 0 <= y + 1 < N and home[x][y + 1] == 0 and home[x + 1][y] == 0 and home[x + 1][y + 1] == 0:
deq.append([x + 1, y + 1, 2])
bfs()
print(count)
๋ฐฉ๋ฒ์ ๊ฐ๋จํ๋ค. ํ์ํ๋ฉด์ ์ขํ์ ๊ฐ์ด ์ํ(๊ฐ๋ก, ์ธ๋ก, ๋๊ฐ์ )๋ฅผ ๋ํ๋ด๋ ๊ฐ๋ ํ์ ๋ฃ์ด์ฃผ๋ฉด ๋๋ค.
๊ทผ๋ฐ ์๊ฐ ์ด๊ณผ๋ ์ ์ค๋ง ํ๊ณ dfs๋ก ํ์ด๋ด.
import sys
count = 0
N = int(input())
home = []
for _ in range(N):
home.append([int(x) for x in sys.stdin.readline().rstrip().split()])
def dfs(x,y,state): # state 0: ๊ฐ๋ก, 1: ์ธ๋ก , 2: ๋๊ฐ์
global count
if x == N - 1 and y == N - 1:
count += 1
return
if state == 0:
if y == N - 1: # ์ด๋๋ถ๊ฐ
return
if 0 <= x < N and 0 <= y + 1 < N and home[x][y + 1] == 0:
dfs(x, y + 1, 0)
if 0 <= x + 1 < N and 0 <= y + 1 < N and home[x][y + 1] == 0 and home[x + 1][y] == 0 and home[x + 1][
y + 1] == 0:
dfs(x + 1, y + 1, 2)
elif state == 1:
if x == N - 1: # ์ด๋๋ถ๊ฐ
return
if 0 <= x + 1 < N and 0 <= y < N and home[x + 1][y] == 0:
dfs(x + 1, y, 1)
if 0 <= x + 1 < N and 0 <= y + 1 < N and home[x][y + 1] == 0 and home[x + 1][y] == 0 and home[x + 1][
y + 1] == 0:
dfs(x + 1, y + 1, 2)
elif state == 2:
if 0 <= x < N and 0 <= y + 1 < N and home[x][y + 1] == 0:
dfs(x, y + 1, 0)
if 0 <= x + 1 < N and 0 <= y < N and home[x + 1][y] == 0:
dfs(x + 1, y, 1)
if 0 <= x + 1 < N and 0 <= y + 1 < N and home[x][y + 1] == 0 and home[x + 1][y] == 0 and home[x + 1][
y + 1] == 0:
dfs(x + 1, y + 1, 2)
dfs(0,1,0)
print(count)
๋ฐฉ๋ฒ์ ์์ bfs๋ก ํ์๋๊ฑฐ๋ ๋๊ฐ๋ค.
๊ทผ๋ฐ pypy๋ก๋ ๋ง์ง๋ง python์ผ๋ก ์ฑ์ ํ๋ ์๊ฐ ์ด๊ณผ๊ฐ ๋ด๋ค.
๊ทธ๋ฅ ๋์ด๊ฐ๋ ค๋ค๊ฐ ์ฐ์ฐํด์ ์ฐพ์๋ณด๋๊น dp๋ก๋ ํด๊ฒฐ๊ฐ๋ฅํด์ dp๋ก ์ต์ข ํด๊ฒฐํ๋ค.
ํ์ด ๊ณผ์
๋จผ์ ๋ฐฐ์ด์ x, y ์ขํ์ ๊ฐ๋ก, ์ธ๋ก, ๋๊ฐ์ ์ํ๋ฅผ ์ ์ฅํ๊ธฐ ์ํด 3์ฐจ์ ๋ฆฌ์คํธ๋ฅผ ์ ์ธํ๋ค.
DP[0][0][1] = 1
for i in range(2,N):
if home[0][i] == 0:
DP[0][0][i] = DP[0][0][i - 1]
์ดํ ์ด๊ธฐ๊ฐ์ ์ค์ ํด์ค๋ค. 1 ํ์ ๊ฐ๋ก ๋ฆฌ์คํธ์๋ง ์ ๋ ฅํด์ฃผ๋ฉด ๋๋ค.
์ค๊ฐ์ 1(๋ฒฝ)์ด ์์ผ๋ฉด ์ดํ ๊ฐ๋ค์ 0 ์ด๋ฏ๋ก ๋ฒฝ์ ๋ง๋๊ธฐ ์ ๊น์ง 1๋ก ๋ฃ์ด์ค๋ค.
for i in range(1,N):
for j in range(1,N):
if home[i][j] == 0 and home[i][j - 1] == 0 and home[i - 1][j] == 0:
DP[2][i][j] = DP[0][i - 1][j - 1] + DP[1][i - 1][j - 1] + DP[2][i - 1][j - 1]
# ๋๊ฐ์ ํ์ดํ ๋๊ธฐ
if home[i][j] == 0:
DP[0][i][j] = DP[0][i][j - 1] + DP[2][i][j - 1]
# ๊ฐ๋ก ํ์ดํ ๋๊ธฐ
DP[1][i][j] = DP[1][i - 1][j] + DP[2][i - 1][j]
# ์ธ๋ก ํ์ดํ ๋๊ธฐ
์ดํ์๋ ์กฐ๊ฑด์ ๋ง๊ฒ DP๋ฆฌ์คํธ์ ๊ฐ์ ๋ฃ์ด์ฃผ๋ฉด ๋๋ค. ๋ฐฑ์ค ๋ฌธ์ ์ค๋ช ์ ์๋ ๊ทธ๋ฆผ์ ๋ณด๋ฉด ๋์๋๋ค.
์ ์ฒด ์ฝ๋
import sys
home = []
N = int(input())
for _ in range(N):
home.append([int(x) for x in sys.stdin.readline().rstrip().split()])
DP = [[[0 for _ in range(N)] for _ in range(N)] for _ in range(3)]
# 0 ๊ฐ๋ก, 1 ์ธ๋ก, 2 ๋๊ฐ์
DP[0][0][1] = 1
for i in range(2,N):
if home[0][i] == 0:
DP[0][0][i] = DP[0][0][i - 1]
#print(DP)
for i in range(1,N):
for j in range(1,N):
if home[i][j] == 0 and home[i][j - 1] == 0 and home[i - 1][j] == 0:
DP[2][i][j] = DP[0][i - 1][j - 1] + DP[1][i - 1][j - 1] + DP[2][i - 1][j - 1]
# ๋๊ฐ์ ํ์ดํ ๋๊ธฐ
if home[i][j] == 0:
DP[0][i][j] = DP[0][i][j - 1] + DP[2][i][j - 1]
# ๊ฐ๋ก ํ์ดํ ๋๊ธฐ
DP[1][i][j] = DP[1][i - 1][j] + DP[2][i - 1][j]
# ์ธ๋ก ํ์ดํ ๋๊ธฐ
print(DP[0][N - 1][N - 1] + DP[1][N - 1][N - 1] + DP[2][N - 1][N - 1])
์์ผ๋ก ์๊ฐ ์ด๊ณผ๊ฐ ๊ฑธ๋ฆฌ๋ฉด bfs๋ณด๋จ dfs๋ dp๋ฅผ ์ฌ์ฉํด ํ์ด์ผ๊ฒ ๋ค.
'๐งฉ Problem Solving > [๋ฐฑ์ค]' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] 7562 ๋์ดํธ์ ์ด๋ (python ํ์ด์ฌ) (0) | 2022.06.18 |
---|---|
[๋ฐฑ์ค] 11404 ํ๋ก์ด๋ (python ํ์ด์ฌ) (0) | 2022.06.18 |
[๋ฐฑ์ค] 10026 ์ ๋ก์์ฝ(python ํ์ด์ฌ) (0) | 2022.06.15 |
[๋ฐฑ์ค] 1655 ๊ฐ์ด๋ฐ๋ฅผ๋งํด์ (python ํ์ด์ฌ) (0) | 2022.06.14 |
[๋ฐฑ์ค] 7569 ํ ๋งํ (python ํ์ด์ฌ) (0) | 2022.06.14 |