結果
| 問題 | No.124 門松列(3) | 
| コンテスト | |
| ユーザー |  バカらっく | 
| 提出日時 | 2019-10-02 11:24:11 | 
| 言語 | Kotlin (2.1.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 3,042 ms / 5,000 ms | 
| コード長 | 2,390 bytes | 
| コンパイル時間 | 16,638 ms | 
| コンパイル使用メモリ | 457,812 KB | 
| 実行使用メモリ | 143,540 KB | 
| 最終ジャッジ日時 | 2024-10-03 05:56:56 | 
| 合計ジャッジ時間 | 48,021 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 26 | 
コンパイルメッセージ
Main.kt:3:10: warning: parameter 'args' is never used
fun main(args:Array<String>) {
         ^
Main.kt:51:115: warning: unnecessary non-null assertion (!!) on a non-null receiver of type Map.Entry<Int, Int>
        var ans = dic[dic.lastIndex].let { it[it.lastIndex] }.let { if(it.isEmpty()) -1 else it.minBy { it.value }!!.value }
                                                                                                                  ^
            
            ソースコード
import java.util.*
fun main(args:Array<String>) {
    val (w,h) = readLine()!!.split(" ").map { it.toInt() }
    Proc(w, h).getAns()
}
class Proc(val w:Int, val h:Int) {
    val map = (0 until h).map { a-> readLine()!!.split(" ").withIndex().map { b-> Pos(b.index, a, b.value.toInt(), w) } }
    val dic = (0 until h).map { (0 until w).map { mutableMapOf<Int, Int>() } }
    public fun getAns() {
        val queue = LinkedList<Task>()
        queue.push(Task(map[0][0], null, 0))
        while (queue.isNotEmpty()) {
            val t = queue.pop()
            val prevKey = t.prev?.let { it.index }?:-1
            if(dic[t.current.y][t.current.x].containsKey(prevKey) && dic[t.current.y][t.current.x][prevKey]!! <= t.step) {
                continue
            }
            dic[t.current.y][t.current.x][prevKey] = t.step
            for(i in (t.current.y - 1..t.current.y + 1)) {
                if(i < 0 || i > map.lastIndex) {
                    continue
                }
                for(j in (t.current.x - 1.. t.current.x + 1)) {
                    if(j < 0 || j > map[i].lastIndex) {
                        continue
                    }
                    if(i == t.current.y && j == t.current.x) {
                        continue
                    }
                    if(i != t.current.y && j != t.current.x) {
                        continue
                    }
                    if(t.prev != null) {
                        if(i == t.prev.y && j == t.prev.x) {
                            continue
                        }
                        val tmp = listOf(t.prev.z, t.current.z, map[i][j].z)
                        if(tmp.distinct().size != 3) {
                            continue
                        }
                        if(tmp.max() != tmp[1] && tmp.min() != tmp[1]) {
                            continue
                        }
                    }
                    queue.push(Task(map[i][j], t.current, t.step + 1))
                }
            }
        }
        var ans = dic[dic.lastIndex].let { it[it.lastIndex] }.let { if(it.isEmpty()) -1 else it.minBy { it.value }!!.value }
        if(ans <= 0) {
            ans = -1
        }
        println(ans)
    }
    class Pos(val x:Int, val y:Int, val z:Int, val w:Int) {
        val index = y * w + x
    }
    class Task(val current:Pos, val prev:Pos?, val step:Int)
}
            
            
            
        