팡세영
Log sey
팡세영
전체 방문자
오늘
어제
  • 분류 전체보기 (74)
    • PS (45)
      • programmers (13)
      • 백준 (29)
    • Android (16)
    • Daily (0)
    • Kotlin (6)
    • Design Pattern (2)
    • Java (1)
    • Flutter (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • 의존성 주입
  • Kotlin
  • 실버
  • TestCode
  • LEVEL2
  • compose
  • 하단네비게이션바
  • 정렬
  • 백준
  • programmers #프로그래머스
  • CustomView
  • 구현
  • 해쉬맵
  • 이분탐색
  • BFS
  • 문자열
  • 안드로이드
  • binding
  • 골드
  • 프로그래머스
  • java
  • flutter
  • 완전탐색
  • mvvm
  • programmers
  • 자바
  • 코틀린
  • Android
  • DFS
  • ArcitecturePattern

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
팡세영

Log sey

백준 촌수 계산(2644)
PS/백준

백준 촌수 계산(2644)

2022. 6. 27. 03:02

촌수 계산(2644)


풀이

  • bfs를 통해 깊이를 세주면 된다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

class FastScanner {
    BufferedReader br;
    StringTokenizer st;

    public FastScanner() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    long nextLong() {
        return Long.parseLong(next());
    }
}


public class Main {
    static int arr[][];
    static boolean visited[];
    static int depth[];

    public static void main(String args[]) throws IOException {
        FastScanner sc = new FastScanner();

        int n = sc.nextInt();   // 전체 사람 수 (정점 수)
        int findX = sc.nextInt();
        int findY = sc.nextInt();
        int m = sc.nextInt();  // 부모 자식 관계 수 (간선 수)

        arr = new int[n+1][n+1];
        visited = new boolean[n+1];
        depth = new int[n+1];

        for(int i=0; i<m; i++){
            int a = sc.nextInt();
            int b = sc.nextInt();
            arr[a][b] = 1;
            arr[b][a] = 1;
        }

        bfs(findX, n);

        if(depth[findY] == 0)
            System.out.println("-1");
        else
           System.out.println(depth[findY]);

    }

    public static void bfs(int start,int n) {
        Queue<Integer> queue = new LinkedList<>();
        queue.add(start);

        while(!queue.isEmpty()){
            int pos = queue.poll();

            for(int i=1; i<=n; i++){
                if(!visited[i] && arr[pos][i] == 1){
                    queue.add(i);
                    visited[i] = true;
                    depth[i] = depth[pos] + 1;
                }
            }
        }


    }

}

'PS > 백준' 카테고리의 다른 글

[백준] Cupid (16460) - Java  (0) 2022.07.07
[백준] 노드 사이 거리 (1240) - Java, bfs  (0) 2022.07.05
[백준 17264] I AM IRONMAN  (0) 2022.06.28
백준 볼링 점수 계산(17215)  (0) 2022.06.27
백준 숨바꼭질(1697)  (0) 2022.06.27
    'PS/백준' 카테고리의 다른 글
    • [백준] 노드 사이 거리 (1240) - Java, bfs
    • [백준 17264] I AM IRONMAN
    • 백준 볼링 점수 계산(17215)
    • 백준 숨바꼭질(1697)
    팡세영
    팡세영
    Android, CS, PS

    티스토리툴바