Solved.ac Class3++
public class Main {
private static int size;
private static boolean[][] data;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
size = Integer.parseInt(br.readLine());
data = new boolean[size][size];
for (int i = 0; i < size; i++) {
String[] split = br.readLine().split(" ");
for (int j = 0; j < size; j++) {
int now = Integer.parseInt(split[j]);
if (now == 0) {
data[i][j] = false;
} else {
data[i][j] = true;
}
}
}
for (int i = 0; i < size; i++) {//start
sb.append(toString(solve(i))).append("\n");
}
System.out.println(sb);
}
private static boolean[] solve(int start) {
Queue<Integer> queue = new LinkedList<>();
boolean[] isVisit = new boolean[size];
for (int i = 0; i < size; i++) {
if (data[start][i]) {
queue.add(i);
isVisit[i] = true;
}
}
while (!queue.isEmpty()) {
int visit = queue.remove();
for (int i = 0; i < size; i++) {
if (data[visit][i]&&!isVisit[i]) {
queue.add(i);
isVisit[i] = true;
}
}
}
return isVisit;
}
private static String toString(boolean[] data) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < size; i++) {
if (data[i]) {
sb.append("1 ");
} else {
sb.append("0 ");
}
}
return sb.toString();
}
}
성공
val size = readln().toInt()
val data = Array(size){ BooleanArray(size) }
fun main() {
val sb = StringBuilder()
for (i in 0..<size) {
val split = readln().split(" ")
for (j in 0..<size) {
val selectData = split[j].toInt()
if (selectData == 0) {
data[i][j] = false
} else {
data[i][j] = true
}
}
}
for (i in 0..<size) {
sb.append(toString(solve(i))).append("\n")
}
print(sb)
}
fun solve(start:Int): BooleanArray {
val queue: Queue<Int> = LinkedList()
val isVisit = BooleanArray(size)
for (i in 0..<size) {
if (data[start][i]) {
queue.add(i)
isVisit[i] = true
}
}
while (queue.isNotEmpty()) {
val visit = queue.remove()
for (i in 0..<size) {
if (data[visit][i] && !isVisit[i]) {
queue.add(i)
isVisit[i] = true
}
}
}
return isVisit
}
fun toString(data: BooleanArray): String {
val sb = StringBuilder()
for (i in 0..<size) {
if (data[i]) {
sb.append("1 ")
} else {
sb.append("0 ")
}
}
return sb.toString()
}