Is there a solution to the stone game using the greedy method?

224 Views Asked by At

It’s a two player game. Both the players play optimally. Given n number of stones, a player can choose either 1 stone or p stones or q stones where 1 < p < q. Suppose player 'A' starts the game then determine who will win the game. The Player who picks the last 1 stone or last p stones or last q stones wins the game.

Now, can this be solved using a greedy approach?

EDIT:

So, I have looked at similar problems which suggest that I build some base cases and start generalising from there. For the cases, n = 0, A loses, n = 1, n = p, n = q; A wins. For other cases, the winning and losing depend on the values of p and q.

If p < n < q, then I would see if n - p is even and if n - p < p. If that is the case then we choose to pick p stones else the only choice is to pick up one stone. But if, n - p > p, then I am not able to understand how to make a greedy choice.

Similarly, if n > q. How would I greedily determine my choice? It would be helpful if you can direct me in constructing an algorithm.