728x90
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
final var br = new BufferedReader(new InputStreamReader(System.in));
final var bw = new BufferedWriter(new OutputStreamWriter(System.out));
final var n = Integer.parseInt(br.readLine());
final var solver = new Solver(n);
for (var i = 0; i < n; i++) {
final var edges = br.readLine().trim().toCharArray();
for (var j = 0; j < n; j++) {
if (edges[j] == 'Y') {
solver.addEdge(i, j);
}
}
}
bw.write(String.valueOf(solver.getMaxConnectionCount()));
bw.flush();
bw.close();
br.close();
}
private static class Solver {
private final boolean[][] graph;
private final int size;
private boolean[][] connectedMap;
private Solver(final int number) {
size = number;
graph = new boolean[number][number];
for (var y = 0; y < number; y++) {
for (var x = 0; x < number; x++) {
graph[y][x] = false;
}
}
}
private void addEdge(final int start, final int end) {
graph[start][end] = true;
}
private void calculateConnectedMap() {
connectedMap = new boolean[size][size];
for (var k = 0; k < size; k++) {
for (var s = 0; s < size; s++) {
for (var e = 0; e < size; e++) {
final var isSelfReference = s == e;
if (isSelfReference) {
continue;
}
final var isPossibleToConnect = graph[s][e] || (graph[s][k] && graph[k][e]);
if (isPossibleToConnect) {
connectedMap[s][e] = true;
}
}
}
}
}
private int getMaxConnectionCount() {
if (Objects.isNull(connectedMap)) {
return 0;
}
this.calculateConnectedMap();
return calculateMaxFriends();
}
private int calculateMaxFriends() {
var maxFriends = Integer.MIN_VALUE;
for (var y = 0; y < size; y++) {
var friends = 0;
for (var x = 0; x < size; x++) {
if (connectedMap[y][x]) {
friends++;
}
}
if (maxFriends < friends) {
maxFriends = friends;
}
}
return maxFriends;
}
}
}
'독서 > 알고리즘' 카테고리의 다른 글
연결 요소의 개수 (0) | 2023.12.15 |
---|---|
백준 1965 상자넣기 (0) | 2022.01.10 |
백준 2110 공유기 설치 (0) | 2021.12.30 |
백준 23352 방탈출 (0) | 2021.12.28 |
백준 1188 음식 평론가 (0) | 2021.12.27 |