diff --git "a/Section07/\352\267\270\353\236\230\355\224\204_\354\265\234\353\213\250\352\261\260\353\246\254_BFS.java" "b/Section07/\352\267\270\353\236\230\355\224\204_\354\265\234\353\213\250\352\261\260\353\246\254_BFS.java" new file mode 100644 index 0000000..f809a47 --- /dev/null +++ "b/Section07/\352\267\270\353\236\230\355\224\204_\354\265\234\353\213\250\352\261\260\353\246\254_BFS.java" @@ -0,0 +1,59 @@ +package 인프런.Section07; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.Queue; +import java.util.Scanner; + +public class 그래프_최단거리_BFS { + static int n, m; + static ArrayList> graph; + static int[] ch, dis; + + public void BFS(int v) { + Queue q = new LinkedList<>(); + ch[v] = 1; + dis[v] = 0; + q.offer(v); + + while(!q.isEmpty()) { + int cv = q.poll(); + for(int nv : graph.get(cv)) { + if(ch[nv] == 0) { //방문 유무 + ch[nv] = 1; + q.offer(nv); + dis[nv] = dis[cv] + 1; + } + } + } + } + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + 그래프_최단거리_BFS t = new 그래프_최단거리_BFS(); + + n = scanner.nextInt(); + m = scanner.nextInt(); + + graph = new ArrayList<>(); + for(int i = 0; i <= n; i++ ){ + graph.add(new ArrayList<>()); + } + + ch = new int[n+1]; + dis = new int[n+1]; + + for(int i = 0; i < m; i++) { + int a = scanner.nextInt(); + int b = scanner.nextInt(); + graph.get(a).add(b); + } + + t.BFS(1); + + for(int i = 2; i <= n; i++) { + System.out.println(i + " : " + dis[i]); + } + } +}