ποΈΒ solution
import sys
from collections import deque
from typing import List
def bfs(y: int, x: int):
time, flag = -1, False
q = deque([[y, x]])
while q:
for _ in range(len(q)):
vy, vx = q.popleft()
if vx+1 >= N or vx+K >= N:
flag = True
break
if graph[vy][vx+1] and not visited[vy][vx+1]: # μμΌλ‘ ν μΉΈ μ΄λ
q.append([vy, vx+1])
visited[vy][vx+1] = True
if vx-1 > time+1 and graph[vy][vx-1] and not visited[vy][vx-1]: # λ€λ‘ ν μΉΈ μ΄λ, κ° μ μλ ꡬμμ 미리 μμν΄μ νμ΄μΌ ν¨
q.append([vy, vx-1])
visited[vy][vx-1] = True
if graph[(vy+1) % 2][vx+K] and not visited[(vy+1) % 2][vx+K]: # μμΌλ‘ ν μΉΈ μ΄λ
q.append([(vy+1) % 2, vx+K])
visited[(vy+1) % 2][vx+K] = True
time += 1
return flag
if __name__ == "__main__":
N, K = map(int, sys.stdin.readline().split())
graph = [list(map(int, sys.stdin.readline().rstrip())) for _ in range(2)]
visited = [[False] * N for _ in range(2)]
print(1) if bfs(0, 0) else print(0)
π‘Β idea
- 맀μ΄λ§λ€ λΈλ μ¬λΌμ§λ κΈ°λ₯ νμ
- λ§€μ΄ λ¨μλ‘ νμ
λ ₯μ λκΈ°
- while & for-loopλ μ΄λ¨μλ‘ νμ
λ ₯ λμ΄λ΄λ ꡬννκΈ° λ§€μ° μ΄λ €μ
- μΌλ°μ μΈ bfs ꡬν체 λμ if-else ꡬ문μΌλ‘ ν νλμ λν λͺ¨λ κ²½μ°μ μκ° ν λ²μ μ²λ¦¬ λλλ‘ κ΅¬ν
- λ€λ‘ κ°λ₯ κ²½μ°μ λν΄μλ§ μμΈμ²λ¦¬
- μκ° μ΄λ§λ€ λΈλμ μμ νλ λ°©λ² X, λ€λ‘ κ°λ €λ λΈλμ΄ λ€μλ²μ μμ μμ μΈμ§ νλ¨
- μ¬λΌμ§ μμ μ΄λ©΄ νμ μ½μ
νμ§ μκ³
pass
Leave a comment