Two players are playing a game of Tower Breakers! Player always moves first, and both players always play optimally.The rules of the game are as follows:
Initially there are towers.
Each tower is of height .
The players move in alternating turns.
In each turn, a player can choose a tower of height and reduce its height to , where and evenly divides .
If the current player is unable to make a move, they lose the game.
Given the values of and , determine which player will win. If the first player wins, return . Otherwise, return .
처음에 너무 다양한 수가 떠올라 지레 겁먹고 어떻게 구현하나 고민을 했다.
결국 다른 코드들을 보니 너무 코드들이 단순했다.
저렇게 단순해도 되나 싶을 정도라서 일단 나도 최대한 단순하게 가정을 한 후 풀었다.
m과 n이 짝수일 경우, 홀수일 경우 이렇게 두가지를 놓고 풀어봤다.
m과 n의 값이 어떻든 짝수,홀수 일때 동일하게 결과가 나올 것이므로
타워의 갯수는 2개, 3개일때
층수는 4일때, 5일 때 이런식으로 간단하게 줄여서 풀어봤다.
결과는 타워가 2개일때는 무조건 플레이어2가 이기고,
타워가 3개일때는 무조건 플레이어 1이 이겼다.
층수는 1만 아니면 상관없었다.
따라서 그에 맞춰 코드를 짰다.
/*
* Complete the 'towerBreakers' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER n
* 2. INTEGER m
*/
int towerBreakers(int n, int m) {
if(m==1) return 2;
if(n%2==0) return 2;
return 1;
}
전혀 예상치 못한 문제이고 예상치 못한 풀이였다.
짝수,홀수로만 나눠 일반화하는 풀이라니
앞으로 너무 다양한 경우의수를 생각하지말고 최대한 단순화 시키는 방법도 고려해야겠다.