Connection Status:
Competition Arena > BoardEscape
SRM 676 · 2015-11-03 · by lg5293 · Graph Theory, String Manipulation
Class Name: BoardEscape
Return Type: String
Method Name: findWinner
Arg Types: (vector<string>, int)
Problem Statement

Problem Statement

Alice and Bob have a rectangular board divided into a grid with r rows and c columns. The grid is described by the String[] s. Each character of s represents one cell. There are four types of cells:

  • 'E' denotes an exit. There may be arbitrarily many exits, possibly zero.
  • 'T' means the square contains a single token. There may be arbitrarily many tokens, possibly zero.
  • '#' denotes an obstacle.
  • '.' denotes an empty cell.

Alice and Bob will play a game on this board. The game is parameterized by the int k. Each token initially has the number k on it. The players will take alternating turns, starting with Alice. A player's turn consists of the following steps:

  1. The player chooses a single token on the board and moves it one square up, down, left, or right. The token cannot go over the edge of the board and it cannot enter a cell with an obstacle. The token may be moved into a cell that already contains other token(s). During the game, arbitrarily many tokens may share the same cell.
  2. If this token is on an exit, it disappears from the board.
  3. Otherwise, subtract one from the number on the token. If the number on the token is zero, remove it from the board. (The numbers on other tokens do not change.)

The first player unable to make a move loses the game.

You are given the String[] s and the int k Compute the winner of the game, assuming both Alice and Bob play optimally. Return the winner's name: either "Alice" or "Bob".

Constraints

  • r,c will be between 1 and 50, inclusive.
  • s will contain exactly r elements, each consisting of c characters.
  • Each character of each element of s will be one of 'T', 'E', '#', or '.'.
  • k will be between 1 and 1,000,000,000, inclusive.
Examples
0)
{"TE"}
2
Returns: "Alice"

Alice can move the token directly to the exit on her first move, making Bob unable to move.

1)
{"E#E",
 "#T#",
 "E#E"}
1000000
Returns: "Bob"

Alice is unable to make the first move in this case.

2)
{"T.T.T.",
 ".E.E.E"}
1
Returns: "Alice"
3)
{"TTE"}
6
Returns: "Alice"
4)
{"..........................",
 "......TTT..TTT..T...T.....",
 ".....T.....T..T.TT.TT.....",
 "......TTT..TTT..T.T.T.....",
 ".........T.T.T..T...T.....",
 "......TTT..T..T.T...T.....",
 "..........................",
 "...E#E#E#E#E#E#E#E#E#E#...",
 "..........................",
 "......TTT..TTT...TTT......",
 ".....T........T.T.........",
 "......TTT.....T..TTT......",
 ".....T...T...T..T...T.....",
 "......TTT....T...TTT......",
 "..........................",
 "...#E#E#E#E#E#E#E#E#E#E...",
 "..........................",
 "....TT...T...T..T.TTT.T...",
 "...T.....T...T..T.T...T...",
 "...T.TT..T...TTTT.TT..T...",
 "...T..T..T...T..T.T.......",
 "....TT...TTT.T..T.T...T...",
 ".........................."}
987654321
Returns: "Bob"

Submissions are judged against all 110 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class BoardEscape with a public method string findWinner(vector<string> s, int k) · 110 test cases · 2 s / 256 MB per case

Submitting as anonymous