This page is still under construction.

Parts of this page are still being built. What you see may change.

How to Move the Beans

Time limit2sMemory limit512 MB

Summary
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 HH rows and WW columns. The grid is cylindrical: its left and right sides are glued together, so columns 11 and WW 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 (r,c)(r, c), 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 (r,c)(r, c), a bean can move one square down (to (r+1,c)(r+1, c), only when r<Hr < H), one square to the right (to (r,c+1)(r, c+1) if c<Wc < W, or to (r,1)(r, 1) if c=Wc = W), or one square to the left (to (r,c−1)(r, c-1) if c>1c > 1, or to (r,W)(r, W) if c=1c = 1).

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 HH and WW (1≤H,W≤10001 \le H, W \le 1000). Then HH lines follow, each a string of length WW. The jj-th character of the ii-th line is # if there is no dish at (i,j)(i, j), . 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 (1,1)(1, 1). Alice moves it to (1,2)(1, 2). Bob's only move is to (2,2)(2, 2). Alice then moves the bean to (2,3)(2, 3), and Bob has no moves left, so Alice wins.

Examples3

  1. Example 1

    Input
    2 3
    B.#
    #..
    
    Expected output
    Alice
    
  2. Example 2

    Input
    1 1
    B
    
    Expected output
    Bob
    
  3. Example 3

    Input
    1 3
    B#.
    
    Expected output
    Alice