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) { 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() for (move in validMove) { when (move) { "S" -> { val state = gameState.copy() state.nextTurnS() nextSteps.add(state) } "R" -> { val state = gameState.copy() state.nextTurnR() nextSteps.add(state) } "L" -> { val state = gameState.copy() state.nextTurnL() nextSteps.add(state) } } } gameState = nextSteps.minBy { it.evaluateScore() }!! } } class GameState { var enemyList = mutableListOf() 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 { 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) println("S") 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) println("R") 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) println("L") 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 } 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 } }