Grid Game
Time limit2sMemory limit1024 MB
Two players alternate moving along right and down paths on a grid of XOR-modified values, and the task is to decide which player wins with optimal play.
- Level
Hard9 of 10
- Topics
- Game theory, Bit manipulation, Dynamic programming, Matrix
- Solved
- No attempts yet
Problem
A grid of size is given. Rows are numbered 1 to from top to bottom, and columns are numbered 1 to from left to right. Cell is the cell at row and column .
Each cell holds either or a non-negative integer . If , the cell is impassable. Otherwise, the cell is passable.
Two players take turns on this grid. On a turn, a player does the following.
- Chooses a cell with a positive integer and stands on it. Let be the integer on this starting cell.
- Chooses a non-negative integer with .
- While standing on cell , replaces with , where is the bitwise XOR.
- If cell or cell is passable, the player must move to one of the passable cells of their choice, then repeat from step 3.
- If the player cannot move, the player leaves the grid and the turn ends.
A player who cannot play on their turn, because no cell holds a positive integer, loses, and the other player wins.
Assuming both players play optimally, determine who wins.
Input
The first line contains two integers and (). Each of the next lines contains integers ( or ).
Output
If the first player wins, print first on a single line. Otherwise, print second on a single line.