Cutting Brownies
Time limit2sMemory limit256 MB
Given a B by D brownie sheet where Harry cuts depth and Vicky cuts breadth, decide if the named starting player has a forced win.
- Level
Medium7 of 10
- Topics
- Game theory, Dynamic programming, Recursion
- Solved
- No attempts yet
Problem
John Horton Conway (born 1937) is a British mathematician. He is well known for inventing the cellular automaton usually called the "Game of Life". This problem comes from a game Conway invented in the 1970s.
The game is played with a rectangular sheet of brownies fresh out of the oven. The players are Harry, who cuts only horizontally, and Vicky, who cuts only vertically. At the start there is a single piece made of connected squares, where is the breadth of the sheet and is its depth.
On each turn a player picks one of the remaining pieces and, if a cut is possible, cuts it into two smaller pieces whose breadth and depth are both integers. A horizontal cut by Harry splits the depth of a piece into two positive integers, and a vertical cut by Vicky splits its breadth into two positive integers. Pieces may not be rotated before or after a cut. A player who cannot cut any remaining piece on their turn loses.
Look at a few examples. The simplest game is . Neither Harry nor Vicky can move, so whoever starts loses. A sheet is a win for Harry no matter who starts, and for the same reason a sheet is a win for Vicky no matter who starts.
A sheet is a loss for whoever starts. If Vicky starts, her only move leaves Harry with and , and once he cuts either piece Vicky is left with , , and no move at all. By symmetry Harry loses if he has to start.
Intuition suggests that Vicky tends to win when the sheet is broader than it is deep, since such a sheet allows more vertical cuts, but look at . If Harry starts, his only possible move leaves Vicky with and , which she wins. If Vicky starts, every move of hers leaves Harry with and . Harry answers and leaves Vicky with , , , which she eventually loses, because the two pieces allow no move and the game is lost by whoever moves in it first.
A sheet is a win for Vicky no matter who starts. If Harry starts, he runs out of moves after his first cut. If Vicky starts, her best move is to cut down the middle, leaving Harry with and , which he loses because each game is lost by whoever moves in it first.
Given the initial size of the sheet and the name of the player who starts, write a program that decides whether the starting player has a strategy that forces a win.
Input
The first line contains an integer (), the number of test cases. Each of the next lines holds one test case: two integers and and a string , separated by spaces. is the initial breadth of the sheet (), is its initial depth (), and is either Harry or Vicky, depending on who moves first.
Output
For each test case, print on one line whether the player who starts can force a win. Print the name of the starting player, followed by can win or cannot win.