DevYoon
[SWEA] 1953. ํ์ฃผ๋ฒ ๊ฒ๊ฑฐ (Python) ๋ณธ๋ฌธ
link ๐ https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5PpLlKAQ4DFAUq
1๏ธโฃ BFS ์ฌ์ฉ
2๏ธโฃ ์ ํ ์ข ์ฐ → ๊ฐ ๋ฐฉํฅ์์ ์ฐ๊ฒฐ ๊ฐ๋ฅํ ํ์ดํ ๋ฒํธ ํ๋์ฝ๋ฉ
3๏ธโฃ 1๋ถํฐ 7๊น์ง, ๋ฒํธ์ ๋ฐ๋ผ ์ฐ๊ฒฐ ๊ฐ๋ฅํ ๋ฐฉํฅ ์ง์
from collections import deque
def bfs(y, x):
q = deque()
q.append((y, x, 1)) # y, x, time
while q:
nowy, nowx, time = q.popleft()
if time == L:
return
directy = [-1, 1, 0, 0] # ์ ํ ์ข ์ฐ
directx = [0, 0, -1, 1]
pipe = [[1, 2, 5, 6], [1, 2, 4, 7], [1, 3, 4, 5], [1, 3, 6, 7]] # ์ ํ ์ข ์ฐ ์ฐ๊ฒฐ ๊ฐ๋ฅํ ํ์ดํ
if arr[nowy][nowx] == 1: # ์ ํ ์ข ์ฐ
temp = [x for x in range(4)]
elif arr[nowy][nowx] == 2: # ์ ํ
temp = [0, 1]
elif arr[nowy][nowx] == 3: # ์ข ์ฐ
temp = [2, 3]
elif arr[nowy][nowx] == 4: # ์ ์ฐ
temp = [0, 3]
elif arr[nowy][nowx] == 5: # ํ ์ฐ
temp = [1, 3]
elif arr[nowy][nowx] == 6: # ํ ์ข
temp = [1, 2]
else: # ์ ์ข
temp = [0, 2]
for t in temp:
dy = directy[t] + nowy
dx = directx[t] + nowx
if 0 <= dy < N and 0 <= dx < M:
if check[dy][dx] != 1 and arr[dy][dx] in pipe[t]:
check[dy][dx] = 1
q.append((dy, dx, time+1))
t = int(input())
for tc in range(1, t+1):
N, M, R, C, L = map(int, input().split()) # ์ธ๋ก ํฌ๊ธฐ, ๊ฐ๋ก ํฌ๊ธฐ, ๋งจํ x์ขํ, ๋งจํ y์ขํ, ์๊ฐ
arr = [list(map(int, input().split())) for _ in range(N)]
check = [[0]*M for _ in range(N)]
cnt = 0
check[R][C] = 1
bfs(R, C)
for i in range(N):
for j in range(M):
if check[i][j] == 1:
cnt += 1
print(f'#{tc} {cnt}')