Moving Chips (Large Input)

Alice and Bob are playing a game called Moving Chips . The game is played on an n × m n \times m board where some cells have some chips on it. Two players move alternately. Each turn consists of one person moving a chip to any cell to its left or any cell to its top. For example, the possible movements of chip A on a 3 × 5 3\times 5 board is:

The last player who moves all the chips to the top left cell wins.

Consider the configuration of a 100 × 100 100\times 100 board in this file. . denotes an empty cell and a digit denotes the number of chips on that cell.

Alice will move first. Assume that both players move optimally, who will win the game?

Clarifications :​

  • The chips can be stacked on top of each other.
  • Only one chip may be moved at a time (not a stack), but a chip can move any number of spaces in a single direction (left or up).
  • The chips can move passing any chips.
Alice Bob

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

Ivan Koswara
Jun 30, 2016

Let the rows be numbered 1 to 100 from top to bottom, and the columns be numbered 1 to 100 from left to right.

First, how can we figure out who wins?

If you're familiar with the game Nim , this is actually very similar to it. Suppose a chip is in row r + 1 r+1 and in column c + 1 c+1 . Interpret this as two Nim piles, one of size r r and one of size c c . If we move the chip to row r + 1 r'+1 (with r < r r' < r ), this is equivalent to reducing the pile of size r r to size r r' . Likewise, if we move the chip to column c + 1 c'+1 , this is equivalent to reducing the pile of size c c to size c c' .

This interpretation is correct:

  • A pile corresponds to a chip and a direction (left or up).
  • Moving a chip left/up means reducing the corresponding pile.
  • We can only move one chip in one direction as a single move; we can only reduce one pile as a single move.
  • When all chips are at the top-left corner, the piles are empty.
  • The winner is the player that moved last, or in other words the loser is the player that cannot move; this is the same as Nim.

Thus, we can interpret this as a game of Nim. And conveniently, we know how to solve Nim; we take the XOR of the sizes of the piles, and if it's 0, then the second player wins, otherwise the first player wins.

Now that we've shown that, we can code it: convert it to a game of Nim, and find the winner.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
data = "<contents of the file>".split()
# now data[r][c] refers to cell in row r+1, column c+1
nim_piles = []
for r in range(100):
    for c in range(100):
        if data[r][c] == ".": continue
        for k in range(int(data[r][c])):
            nim_piles.append(r)
            nim_piles.append(c)
result = 0
for pile in nim_piles: result = result ^ pile
print("Alice" if result != 0 else "Bob")
# prints Bob

Moderator note:

Good approach using Nim to solve this combinatorial game.

It would be more clear if the rule specifies that in one turn you are allowed to move only a single chip. But, the chip can be moved any number of cells either to the left or to the top.

Janardhanan Sivaramakrishnan - 4 years, 11 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...