https://www.acmicpc.net/problem/11403
저는 플루이드 워샬을 통해 문제를 해결했습니다.
플루이드 워샬을 통해 각각의 지점에 연결할 수 있는지를 판단하였습니다.
package BOJ.etc;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.function.Function;
public class BOJ_11403 {
private static final int CONNECT = 1;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
Function<String,Integer> stoi = Integer::parseInt;
int n = stoi.apply(st.nextToken());
int[][] map = new int[n][n];
for(int i = 0 ; i < n ; i++){
st = new StringTokenizer(br.readLine());
for(int j = 0 ; j < n ; j++){
map[i][j] = stoi.apply(st.nextToken());
}
}
cal(map,n);
for(int i = 0 ; i < n ; i++){
for(int j = 0 ; j < n ; j++){
System.out.print(map[i][j]+" ");
}
System.out.println();
}
}
private static void cal(int[][] map, int n) {
for(int k = 0 ; k < n ; k++){
for(int i = 0 ; i < n ; i++){
for(int j = 0 ; j < n ; j++){
if(map[i][k] == CONNECT && map[k][j] == CONNECT){
map[i][j] = CONNECT;
}
}
}
}
}
}
'알고리즘' 카테고리의 다른 글
백준 6146번 신아를 만나러 (JAVA) (0) | 2023.06.21 |
---|---|
백준 21937번 작업 (JAVA) (0) | 2023.06.20 |
백준 25634번 전구 상태 뒤집기 (JAVA) (0) | 2023.06.18 |
백준 28107번 회전초밥 (JAVA) (0) | 2023.06.17 |
백준 16469번 소년 점프 (JAVA) (0) | 2023.06.16 |