Baekjoon - 1149

Tadap·2023년 11월 2일
0

Baekjoon

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

문제

Solved.ac Class4

1차시도

public class Main {
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		int houseCount = Integer.parseInt(br.readLine());
		int[][] data = new int[houseCount][3];
		int minValue = Integer.MAX_VALUE;

		for (int i = 0; i < houseCount; i++) {
			String[] splitData = br.readLine().split(" ");
			data[i][0] = Integer.parseInt(splitData[0]);
			data[i][1] = Integer.parseInt(splitData[1]);
			data[i][2] = Integer.parseInt(splitData[2]);
		}

		for (int i = 1; i < houseCount; i++) {
			data[i][0] += Math.min(data[i - 1][1], data[i - 1][2]);
			data[i][1] += Math.min(data[i - 1][0], data[i - 1][2]);
			data[i][2] += Math.min(data[i - 1][0], data[i - 1][1]);
		}

		for (int i = 0; i < 3; i++) {
			minValue = Math.min(data[houseCount - 1][i], minValue);
		}
		System.out.println(minValue);
	}
}

성공

ToKotlin

fun main() {
    val houseCount = readln().toInt()
    val data = Array(houseCount) { IntArray(3) }
    var minValue = Int.MAX_VALUE

    for (i in 0..<houseCount) {
        val splitData = readln().split(" ").map { s -> s.toInt() }.toList()
        data[i][0] = splitData[0]
        data[i][1] = splitData[1]
        data[i][2] = splitData[2]
    }

    for (i in 1..<houseCount) {
        data[i][0] += min(data[i - 1][1], data[i - 1][2]);
        data[i][1] += min(data[i - 1][0], data[i - 1][2]);
        data[i][2] += min(data[i - 1][0], data[i - 1][1]);
    }

    for (i in 0..<3) {
        minValue = min(data[houseCount - 1][i], minValue)
    }

    print(minValue)
}

post-custom-banner

0개의 댓글