How to Move the Beans
Time limit2sMemory limit512 MB
On a cylindrical grid of dishes, Alice and Bob alternately move beans that may never return to their own previous squares; the task is to find the winner under optimal play.
- Level
Hard9 of 10
- Topics
- Game theory, Graph, DFS
- Solved
- No attempts yet
Problem
A grid has rows and columns. The grid is cylindrical: its left and right sides are glued together, so columns and are neighbors.
Some grid squares contain dishes. Initially, beans are placed on some of these dishes, with at most one bean per dish. Later in the game, a dish may hold any number of beans.
Alice and Bob take turns, and Alice moves first. On each turn, a player picks any bean, denotes its current row and column by , and moves it by these rules:
- A bean can be moved only to a square with a dish.
- A bean cannot be moved to a square where this same bean was before. All beans are distinguishable.
- From , a bean can move one square down (to , only when ), one square to the right (to if , or to if ), or one square to the left (to if , or to if ).
A player who cannot move any bean on their turn loses. Determine who wins if both players play optimally.
Input
The first line contains two integers and (). Then lines follow, each a string of length . The -th character of the -th line is # if there is no dish at , . if there is an empty dish, and B if there is a dish with exactly one bean.
The grid is not guaranteed to contain all three characters. For example, a grid with no beans is valid.
Output
Print Alice if Alice wins when both players play optimally. Otherwise, print Bob.
Hint
In the first example, the only bean starts at . Alice moves it to . Bob's only move is to . Alice then moves the bean to , and Bob has no moves left, so Alice wins.