백준 1058 java

magicdrill·2024년 10월 18일
0

백준 문제풀이

목록 보기
467/655

백준 1058 java

플로이드 워셜을 사용한 문제중 쉬운편이었다....

import java.util.Scanner;

import static java.lang.Math.min;

public class bj1058 {
    static Scanner scanner = new Scanner(System.in);
    static int friendRelation [][];
    static final int INF = 1000000000;

    public static void main(String[] args) {
        inputData();
        findAnswer();

        scanner.close();
    }

    public static void inputData()
    {
        System.out.println("inputData()");
        int N;
        int i, j;

        N = scanner.nextInt();
        friendRelation = new int[N + 1][N + 1];

        for(i = 1; i < N + 1; i++)
        {
            String str;
            str = scanner.next();
            for(j = 1; j < N + 1; j++)
            {
                if(i == j)
                {
                    continue;
                }
                char ch = str.charAt(j - 1);
                if(ch == 'N')
                {
                    friendRelation[i][j] = INF;
                }
                else
                {
                    friendRelation[i][j] = 1;
                }
            }
        }

        System.out.println("inputData 결과");
        for(i = 1; i < N + 1; i++)
        {
            for(j = 1; j < N + 1; j++)
            {
                System.out.print(friendRelation[i][j] + " ");
            }
            System.out.println();
        }
    }

    public static void findAnswer()
    {
        System.out.println("findAnswer()");
        int max = 0, N = friendRelation.length, count;
        int i, j;

        floydWarshall(friendRelation);//플로이드 워셜 수행
        for(i = 1; i < N; i++)
        {
            count = 0;
            for(j = 1; j < N; j++)
            {
                if(friendRelation[i][j] == 2 || friendRelation[i][j] == 1)
                {
                    count++;
                }
            }
            max = Math.max(max, count);
        }

        System.out.println(max);
    }

    public static void floydWarshall(int [][] friendRelation)
    {
        System.out.println("floydWarshall()");
        int i, j, k, N = friendRelation.length;

        for(k = 1; k < N; k++)
        {
            for(i = 1; i < N; i++)
            {
                for(j = 1; j < N; j++)
                {
                    if(j == k)
                    {
                        ;
                    }
                    else
                    {
                        friendRelation[i][j] = Math.min(friendRelation[i][j], friendRelation[i][k] + friendRelation[k][j]);
                    }
                }
            }
        }

        System.out.println("floydWarshall 결과");
        for(i = 1; i < N; i++)
        {
            for(j = 1; j < N; j++)
            {
                System.out.print(friendRelation[i][j] + " ");
            }
            System.out.println();
        }
    }
}

0개의 댓글