카테고리 없음

[구름 | Java Lv.3] 심리적 거리감 (탐색) - 미완성

Trudy | 송연 2024. 6. 17. 02:58

문제

https://level.goorm.io/exam/195775/%EC%8B%AC%EB%A6%AC%EC%A0%81-%EA%B1%B0%EB%A6%AC%EA%B0%90/quiz/1

 

구름LEVEL

난이도별 다양한 문제를 해결함으로써 SW 역량을 향상시킬 수 있습니다.

level.goorm.io

 


최종 코드 - 테스트케이스 다수 실패

package week7.baek.search;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public class PsyDistance {
    static List<List<Integer>> bridge;

    public static int[] bfs(int start, int n){
        int[] distances = new int[n];
        Arrays.fill(distances, -1);  // -1로 초기화 (경로가 없음을 나타냄)
        Queue<Integer> queue = new LinkedList<>();

        queue.offer(start);
        distances[start] = 0;

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

            for (int neighbor : bridge.get(current)) {
                if (distances[neighbor] == -1) {  // 방문하지 않은 노드만
                    distances[neighbor] = distances[current] + 1;
                    queue.offer(neighbor);
                }
            }
        }

        return distances;

    }

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String input = br.readLine();
        int N = Integer.parseInt(input.split(" ")[0]);
        int M = Integer.parseInt(input.split(" ")[1]);
        int K = Integer.parseInt(input.split(" ")[2]);

        //인접 리스트로 그래프 표현 - 방향 있음
        bridge = new ArrayList<>();
        for (int i = 0; i < M; i++) {
            bridge.add(new ArrayList<>());
        }

        for (int i = 0; i < M; i++) {
            input = br.readLine();
            int from = Integer.parseInt(input.split(" ")[0]);
            int to = Integer.parseInt(input.split(" ")[1]);
//            System.out.println("bridge = " + bridge[i][0] + " " + bridge[i][1]);
            bridge.get(from - 1).add(to - 1);
        }

        int[] psyDistance = new int[N];
        for (int i = 0; i < N; i++) {
            psyDistance = bfs(K - 1, N);
        }

        for (int i = 0; i < N; i++) {
            psyDistance[i] += Math.abs(i - (K-1));
        }

        int maxDistance = Integer.MAX_VALUE;
        int index = 0;
        for (int i = 0; i < N; i++) {
            if(maxDistance < psyDistance[i]){
                maxDistance = psyDistance[i];
                index = i;
            }
        }

        System.out.println(index+1);
    }
}