Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions Algorithms/kotlin/Game_Theory-Misère_Nim.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import java.util.*
import kotlin.collections.*
import kotlin.io.*
import kotlin.text.*

fun misereNim(s: Array<Int>): String {

// 1. 돌의 개수와 전체 pile의 개수가 같다면 모든 파일에 1개의 돌이 있는 것이다.
// 1-1. 따라서, 홀수면 Second가 승리한다.
s.reduce { acc, i -> acc + i }.let {
if (it == s.size) {
return when(it % 2) {
0 -> "First"
else -> "Second"
}
}
}

// 2. 일반적인 Nim game의 승자와 패자가 반대가 된다. 따라서 값이 0이면 Second가 승리한다.
s.reduce { acc, i -> acc xor i }.let {
return when(it) {
0 -> "Second"
else -> "First"
}
}
}

fun main(args: Array<String>) {
val scan = Scanner(System.`in`)

val t = scan.nextLine().trim().toInt()

for (gItr in 1..t) {
val n = scan.nextLine().trim().toInt()

val s = scan.nextLine().split(" ").map{ it.trim().toInt() }.toTypedArray()

val result = misereNim(s)
println(result)
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,5 @@ All about hackerrank
- Game_Theory-A_Chessboard_Game.kt
- Game_Theory-Game_of_Stones.kt
- Game_Theory-Introduction_to_Nim_Game.kt
- Game_Theory-Misère_Nim.kt
- Game_Theory-Tower_Breakers.kt