diff --git a/Algorithms/kotlin/Game_Theory-Nimble_Game.kt b/Algorithms/kotlin/Game_Theory-Nimble_Game.kt new file mode 100644 index 0000000..268748b --- /dev/null +++ b/Algorithms/kotlin/Game_Theory-Nimble_Game.kt @@ -0,0 +1,35 @@ +import java.util.* +import kotlin.collections.* +import kotlin.io.* +import kotlin.text.* + +fun nimbleGame(s: Array): String { + + var nim = 0 + + // 1. 돌의 개수가 홀수인 인덱스 값을 xor하여 균형상태인지 확인한다. + s.forEachIndexed { index, i -> + if ( i % 2 == 1) { + nim = nim.xor(index) + } + } + + // 2. 균형상태라면 Second가 승리한다. + return if (nim == 0) "Second" + else "First" +} + +fun main(args: Array) { + val scan = Scanner(System.`in`) + + val t = scan.nextLine().trim().toInt() + + for (tItr in 1..t) { + val n = scan.nextLine().trim().toInt() + + val s = scan.nextLine().split(" ").map{ it.trim().toInt() }.toTypedArray() + + val result = nimbleGame(s) + println(result) + } +} \ No newline at end of file diff --git a/README.md b/README.md index a98ead7..07f3b0e 100644 --- a/README.md +++ b/README.md @@ -87,4 +87,5 @@ All about hackerrank - Game_Theory-Game_of_Stones.kt - Game_Theory-Introduction_to_Nim_Game.kt - Game_Theory-Misère_Nim.kt +- Game_Theory-Nimble_Game.kt - Game_Theory-Tower_Breakers.kt \ No newline at end of file