문제 : https://www.acmicpc.net/problem/11403 

 

11403번: 경로 찾기

가중치 없는 방향 그래프 G가 주어졌을 때, 모든 정점 (i, j)에 대해서, i에서 j로 가는 경로가 있는지 없는지 구하는 프로그램을 작성하시오.

www.acmicpc.net

 

솔루션 : 

단순 BFS진행

 

 

소스코드 : 

N = int(input())

#데이터 입력받기
temp =[]
for i in range(N) :
    temp.append( list(map(int,input().split()) ))

visited = [[ False for i in range(N)]  for j in range(N) ]  
graph =  [ [] for w in range(N)]

#단방향 그래프 형태로 표현하기
for i in range(N) :
    for j in range(N) :
        if temp[i][j] == 1 :
            graph[i].append(j)



#큐 구현하기
from collections import deque

def bfs(started, graph) :
    Q = deque()
    Q.append(graph[started ])
    
    while Q :
        next_node  = Q.popleft()
        # print(f'next node is {next_node}')

        for i in next_node :
            # print(started, i )
            if visited[started][i] == False :
                visited[started][i] = True 
                Q.append(graph[i])
        # print(Q)
        # print(visited)
        # print("\n", end='')


#처리과정
for i in range(N) :
    bfs(i,graph)


#출력처리
for i in visited:
    for j in i :
        if j == True :
            print("1", end=' ')
        else :
            print("0", end=' ')
    print("\n", end='')

'CodingTest' 카테고리의 다른 글

SWEA 1234 [S/W 문제해결 기본] 10일차 - 비밀번호  (0) 2022.06.05
SWEA 14178. 1차원 정원 파이썬  (0) 2022.06.05
[BOJ] 20922 겹치는건 싫어  (0) 2022.02.19
[BOJ] 14502 연구소  (0) 2022.02.19
[BOJ] 10026 적록색약  (0) 2022.02.19
jjongguet