結果

問題 No.179 塗り分け
ユーザー IJKTanabe
提出日時 2019-03-24 18:49:08
言語 Kotlin
(2.1.0)
結果
AC  
実行時間 877 ms / 3,000 ms
コード長 2,201 bytes
コンパイル時間 15,768 ms
コンパイル使用メモリ 439,732 KB
実行使用メモリ 92,776 KB
最終ジャッジ日時 2024-07-23 14:58:21
合計ジャッジ時間 39,565 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 6
other AC * 40
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:1:10: warning: parameter 'args' is never used
fun main(args: Array<String>) {
         ^
Main.kt:7:39: warning: name shadowed: it
        it.mapIndexedNotNull { index, it ->
                                      ^
Main.kt:28:5: warning: parameter 'cellsList' is never used
    cellsList: List<List<String>>,
    ^
Main.kt:31:5: warning: parameter 'W' is never used
    W: Int,
    ^

ソースコード

diff #

fun main(args: Array<String>) {
    val (H, W) = readLine()!!.split(" ").map(String::toInt)
    // 入力を各セルごとの二次元リストに
    val cellsList: List<List<String>> = List(H) { readLine()!!.chunked(1) }
    // 行ごとに、黒色セルのインデックスのリストを取得
    val blackIndices = cellsList.map {
        it.mapIndexedNotNull { index, it ->
            if (it == "#") (index) else null
        }
    }

/*
    //Check Print
    cellsList.forEach { println(it) }
    blackIndices.forEach { println(it) }
*/

    for (vecY in (-H + 1)..(H - 1)) for (vecX in (-W + 1)..(W - 1)) {
        if (tryPaint(cellsList, blackIndices, H, W, vecY, vecX)) {
            println("YES")
            return
        }
    }
    println("NO")
}

fun tryPaint(
    cellsList: List<List<String>>,
    blackIndices: List<List<Int>>,
    H: Int,
    W: Int,
    vecY: Int,
    vecX: Int
): Boolean {

    //val cells = cellsList.map { it.toMutableList() }
    val rangeY =
        if (vecY < 0)
            ((H - 1) downTo 0).toList()
        else
            (0 until H).toList()

    val indices =
        if (vecX < 0)
            blackIndices.map { it.reversed().toMutableList() }
        else
            blackIndices.map { it.toMutableList() }

    var flg: Boolean? = null

/*
    //Check Print
    println("vec = $vecY, $vecX")
    println("Y_range = $rangeY")
    println("blackIndices =")
    indices.forEach { println(it) }
*/

    for (y in rangeY) {
        while (indices[y].isNotEmpty()) {
            val x = indices[y].removeAt(0)
            val trgY = y + vecY
            val trgX = x + vecX

            //cells[y][x] = "R"

            if (rangeY.contains(trgY) && indices[trgY].contains(trgX)) {
                //cells[trgY][trgX] = "B"
                indices[trgY].remove(trgX)
                if (flg == null) flg = true
            } else {
                //flg = false
                return false
            }
        }
    }

/*
    //Check Print
    cells.forEach { println(it.joinToString("")) }
    println(flg)
    println(" ")
*/

    return flg ?: false
}
0