Notice
Recent Posts
Recent Comments
Link
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
Tags
more
Archives
Today
Total
관리 메뉴

main

[백준-11403번/Java] 경로찾기 본문

Algorithm

[백준-11403번/Java] 경로찾기

1984 2022. 11. 17. 01:45

* 그래프 알고리즘 공부해서 다시 짜기

import java.io.*;
import java.util.*;

public class Main {

	public static void main(String[] args) throws IOException {
		Scanner in = new Scanner(System.in);

		int N = in.nextInt();

		int[][] graph = new int[N][N];

		// 그래프의 인접 행렬
		for (int i = 0; i < graph.length; i++) {
			for (int j = 0; j < graph.length; j++) {
				graph[i][j] = in.nextInt();
			}
		}

		// 2번 반복해야 정답 (이유는 모르겠음.)
		int repeat = 2;
		while (repeat > 0) {
			// 출발-도착, 도착 정점에서 갈 수 있는 정점 확인
			for (int i = 0; i < graph.length; i++) {
				for (int j = 0; j < graph.length; j++) {
					if (graph[i][j] == 1) {
						for (int k = 0; k < graph.length; k++) {
							if (graph[j][k] == 1) {
								graph[i][k] = 1;
							}
						}
					}
				}
			}
			repeat--;
		}

		for (int i = 0; i < graph.length; i++) {
			for (int j = 0; j < graph.length; j++) {
				System.out.print(graph[i][j] + " ");
			}
			System.out.println();
		}

	}

}
728x90

'Algorithm' 카테고리의 다른 글

[백준-11047번/Java] 동전 0  (0) 2022.11.17
[백준-7576번/Java] 토마토  (0) 2022.11.17
[백준-17219번/Java] 비밀번호 찾기  (0) 2022.11.16
[백준-1932번/Java] 정수 삼각형  (0) 2022.11.16
[백준-1699번/Java] 제곱수의 합  (0) 2022.11.15
Comments