結果

問題 No.5017 Tool-assisted Shooting
ユーザー エセ賢者エセ賢者
提出日時 2023-08-03 13:52:14
言語 Kotlin
(2.1.0)
結果
AC  
実行時間 859 ms / 2,000 ms
コード長 3,954 bytes
コンパイル時間 20,526 ms
コンパイル使用メモリ 430,176 KB
実行使用メモリ 75,344 KB
スコア 262,795
平均クエリ数 814.46
最終ジャッジ日時 2023-08-03 13:53:59
合計ジャッジ時間 94,592 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 100
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:10:10: warning: parameter 'args' is never used
fun main(args: Array<String>) {
         ^
Main.kt:46:69: warning: unnecessary non-null assertion (!!) on a non-null receiver of type Pair<String, GameState>
        val minState = nextSteps.minBy { it.second.evaluateScore() }!!
                                                                    ^
Main.kt:117:23: warning: 'sumBy((T) -> Int): Int' is deprecated. Use sumOf instead.
            enemyList.sumBy { it.h }
                      ^

ソースコード

diff #

private fun next() = readLine()!!
private fun nextInt() = next().toInt()
private fun nextLong() = next().toLong()
private fun nextDouble() = next().toDouble()
private fun nextList() = next().split(" ")
private fun nextIntList() = next().split(" ").map { it.toInt() }.toIntArray()
private fun nextLongList() = next().split(" ").map { it.toLong() }.toLongArray()
private fun nextDoubleList() = next().split(" ").map { it.toDouble() }.toDoubleArray()

fun main(args: Array<String>) {
    var gameState = GameState()

    while (!gameState.isDone()) {
        val n = nextInt()
        if (n == -1) {
            return
        }
        for (i in 0 until n) {
            val hpx = nextIntList()
            gameState.addEnemy(hpx)
        }

        val validMove = gameState.validMove()
        val nextSteps = mutableListOf<Pair<String, GameState>>()
        for (move in validMove) {
            when (move) {
                "S" -> {
                    val state = gameState.copy()
                    state.nextTurnS()
                    nextSteps.add(Pair("S", state))
                }

                "R" -> {
                    val state = gameState.copy()
                    state.nextTurnR()
                    nextSteps.add(Pair("R", state))
                }

                "L" -> {
                    val state = gameState.copy()
                    state.nextTurnL()
                    nextSteps.add(Pair("L", state))
                }
            }
        }
        val minState = nextSteps.minBy { it.second.evaluateScore() }!!
        gameState = minState.second
        println(minState.first)
    }
}

class GameState {
    var enemyList = mutableListOf<Enemy>()
    var own = Own(12, 0)
    var turn = 0

    fun addEnemy(hpx: IntArray) {
        enemyList.add(Enemy(hpx[2], 59 + turn, hpx[0], hpx[1]))
    }

    fun isDone(): Boolean {
        return turn >= 1000
    }

    fun validMove(): Array<String> {
        val move = mutableListOf("S")
        if (enemyList.none { it.x == own.x + 1 && it.y == own.y }) {
            move.add("R")
        }
        if (enemyList.none { it.x == own.x - 1 && it.y == own.y }) {
            move.add("L")
        }
        return move.toTypedArray()
    }

    fun nextTurnS() {
        turn++
        own = Own(own.x, own.y + 1)
        val first = enemyList.firstOrNull { it.isAlive() && it.x == own.x && it.y > own.y } ?: return
        first.hit(own.level())
        if (!first.isAlive()) {
            own.defeat(first.p)
        }
    }

    fun nextTurnR() {
        turn++
        own = Own(own.x + 1, own.y + 1)
        val first = enemyList.firstOrNull { it.isAlive() && it.x == own.x && it.y > own.y } ?: return
        first.hit(own.level())
        if (!first.isAlive()) {
            own.defeat(first.p)
        }
    }

    fun nextTurnL() {
        turn++
        own = Own(own.x - 1, own.y + 1)
        val first = enemyList.firstOrNull { it.isAlive() && it.x == own.x && it.y > own.y } ?: return
        first.hit(own.level())
        if (!first.isAlive()) {
            own.defeat(first.p)
        }
    }

    fun copy(): GameState {
        val gameState = GameState()
        gameState.enemyList = enemyList.map { Enemy(it.x, it.y, it.h, it.p) }.toMutableList()
        gameState.own = own
        gameState.turn = turn
        return gameState
    }

    fun evaluateScore(): Int {
        val collision = enemyList.filter { it.x == own.x && (it.y == own.y || it.y == own.y + 1) }
        return if (collision.isEmpty()) {
            enemyList.sumBy { it.h }
        } else {
            Int.MAX_VALUE
        }
    }
}

class Enemy(var x: Int, var y: Int, var h: Int, var p: Int) {
    fun isAlive(): Boolean {
        return h > 0
    }

    fun hit(damage: Int) {
        h -= damage
    }
}

class Own(var x: Int, var y: Int) {
    var p = 100
    fun defeat(power: Int) {
        p += power
    }

    fun level(): Int {
        return p / 100
    }
}
0