코딩테스트/SWEA

[SWEA][BFS] 1226. 미로1

박소민 2023. 4. 10. 11:30
1226. 미로1
 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

  • 내 풀이
# 0: 길 1: 벽
# 시작점 1,1
from collections import deque

def bfs(i,j):
    visited[i][j]=True
    queue.append((i,j))

    while queue:
        x,y=queue.popleft()

        for i in range(4):
            nx=x+dx[i]
            ny=y+dy[i]

            if 1<=nx<15 and 1<=ny<15 and graph[nx][ny]!=1 and visited[nx][ny]==False:
                visited[nx][ny]=True;
                queue.append((nx,ny))



for _ in range(10):
    tc=int(input())
    graph=[list(map(int,input().rstrip())) for _ in range(16)]
    dx=[-1,1,0,0]
    dy=[0,0,-1,1]
    queue=deque()
    visited=[[False for _ in range(16)] for _ in range(16)]

    for i in range(16):
        for j in range(16):
            if graph[i][j]==2:
                bfs(i,j)
            elif graph[i][j]==3:
                endX=i
                endY=j

    result=0
    if visited[endX][endY]==True:
        result=1

    print(f'#{tc} {result}')