Skip to content

Latest commit

 

History

History
112 lines (92 loc) · 3.16 KB

17140번 - 이차원 배열과 연산.md

File metadata and controls

112 lines (92 loc) · 3.16 KB
import java.io.BufferedReader
import java.io.InputStreamReader
import java.util.*
import kotlin.math.max

fun main() = with(BufferedReader(InputStreamReader(System.`in`))) {
    val (r, c, target) = readLine().split(" ").map(String::toInt)
    val graph = Array(100){
        IntArray(100)
    }
    var rowSize = 3
    var colSize = 3
    var answer = 0
    val countArray = IntArray(101)

    repeat(3) {
        val st = StringTokenizer(readLine())
        graph[it][0] = st.nextToken().toInt()
        graph[it][1] = st.nextToken().toInt()
        graph[it][2] = st.nextToken().toInt()
    }

    if (graph[r-1][c-1] == target) {
        print(0)
        return
    }

    while (answer < 100) {
        answer++
        // 행연산
        if (rowSize >= colSize){
            val prevColSize = rowSize
            colSize = 0

            for (row in 0 until rowSize){
                val numberSet = HashSet<Int>()

                for (col in 0 until prevColSize) {
                    val number = graph[row][col]
                    if (number == 0) continue
                    numberSet.add(number)
                    countArray[number]++
                }

                val pairList: List<Pair<Int, Int>> = numberSet.map{ num ->
                    val count = countArray[num]
                    countArray[num] = 0
                    Pair(num, count)
                }.sortedWith(compareBy<Pair<Int, Int>> { it.second }.thenBy {it.first} )

                for (i in pairList.indices) {
                    val (num, count) = pairList[i]
                    graph[row][2 * i] = num
                    graph[row][2 * i + 1] = count
                }

                val currentColSize = pairList.size * 2
                for (i in currentColSize until 100) {
                    graph[row][i] = 0
                }

                colSize = max(colSize, currentColSize)
            }
        }

        // 열연산
        else{
            val prevRowSize = rowSize
            rowSize = 0

            for (col in 0 until colSize){
                val numberSet = HashSet<Int>()

                for (row in 0 until prevRowSize) {
                    val number = graph[row][col]
                    if (number == 0) continue
                    numberSet.add(number)
                    countArray[number]++
                }

                val pairList: List<Pair<Int, Int>> = numberSet.map{ num ->
                    val count = countArray[num]
                    countArray[num] = 0
                    Pair(num, count)
                }.sortedWith(compareBy<Pair<Int, Int>> { it.second }.thenBy {it.first} )

                val currentRowSize = pairList.size * 2

                for (i in pairList.indices) {
                    val (num, count) = pairList[i]
                    graph[2 * i][col] = num
                    graph[2 * i + 1][col] = count
                }

                for (i in currentRowSize until 100){
                    graph[i][col] = 0
                }
                rowSize = max(rowSize, currentRowSize)

            }
        }

        if (graph[r-1][c-1] == target) {
            print(answer)
            return
        }
    }

    print(-1)
}