Baekjoon - 11403

Tadap·2023년 10월 22일
0

Baekjoon

목록 보기
61/94
post-custom-banner

문제

Solved.ac Class3++

1차시도

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();
	}
}

성공

ToKotlin

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()
}

post-custom-banner

0개의 댓글