StoneGameStrategist
SRM 309 · 2006-06-28 · by _efer_
Problem Statement
You and a friend are playing a game in which you take turns removing stones from piles. Initially, every pile has at least as many stones as the pile to its left. This property must be maintained throughout the game. On each turn, you remove one or more stones from a single pile. You and your friend alternate turns until it is no longer possible to make a valid move. The last player to have made a move wins the game. Note that if you remove all the stones from a pile, it is still considered a pile.
You are said to have made a "winning move" if after making that move, you can eventually win no matter what your friend does. You are given a
Constraints
- piles will contain between 1 and 50 elements, inclusive.
- Each element of piles will be between 1 and 1000, inclusive.
- piles will be sorted in non-descending order.
{6,6}
Returns: "YOU LOSE"
You can't win this game. Your friend will force you to take stones from the first pile at each turn, and then mirror your move by taking the same number of stones from the second pile.
{6,12}
Returns: "TAKE 6 STONES FROM PILE 1"
If you take 6 stones from the second pile, you can follow the strategy outlined above.
{3,6,6}
Returns: "TAKE 3 STONES FROM PILE 0"
{3,5,9,11,16}
Returns: "TAKE 2 STONES FROM PILE 0"
{2,5,7,11,13,18}
Returns: "TAKE 2 STONES FROM PILE 1"
Submissions are judged against all 149 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class StoneGameStrategist with a public method string play(vector<int> piles) · 149 test cases · 2 s / 256 MB per case