Under construction

Still being built. Things may change

2048

Interview

Time limit2sMemory limit512 MB

Summary
Simulate a sequence of 2048 moves, applying gravity, merging equal tiles once per move, and adding merge values to the score.
Level

Medium4 of 10

Topics
Simulation, Implementation, Array, Matrix
Solved
No attempts yet

Problem

2048 is a sliding block puzzle game. The goal is to slide numbered tiles on a grid and combine them to create a tile with the number 2048. It is played on a 4×4 grid, and the numbered tiles slide smoothly when the player moves them with the four arrow keys. Every turn, a new tile with a value of either 2 or 4 appears at random in an empty cell of the board. Tiles slide as far as possible in the chosen direction until another tile or the edge of the grid stops them. If two tiles with the same number collide while moving, they merge into a single tile whose value is the sum of the two. The merged tile cannot merge with another tile again during the same move. The user's score starts at zero, and each time two tiles merge it increases by the value of the newly created tile.

Write a program that takes the current user score, M moves together with the random numbers that appear (and their cells on the grid), and the current grid values, and prints the user's score after the moves.

Input

The first line contains the current user score. 0 ≤ S ≤ 500

The next line contains the next M moves. 0 ≤ M ≤ 10

  • [move][2|4][row][column], for example L200: the left arrow is pressed and then a new 2 appears at 0x0
  • Move: L for left, U for up, R for right, D for down

The next line contains the current grid content as a flattened string.

  • From the top left (0x0) to the bottom right (3x3)
  • Each value is 2, 4, 8, 16, 32, 64, 128, 256, 512, or 0, separated by spaces. 0 marks an empty cell.

Output

Print the user's score after the given moves.

Hints

The user's score is 200.

The 2048 grid for the input "0 2 0 0 0 4 0 0 2 8 2 0 8 32 16 2" is shown below.

The input moves are: L213,D202,R211,D210,D200,D212,R410,R200,D401,L212

L213: Left, a new 2 at 1x3. No mergeD202: Down, a new 2 at 0x2, 4 at 3x3R211: Right, a new 2 at 1x1, 4 at 0x3
D210: Down, a new 2 at 1x0, 8 at 1x3, 4 at 2x1D200: Down, a new 2 at 0x0. No mergeD212: Down, a new 2 at 1x2, 4 at 2x0
R410: Right, a new 4 at 1x0, 8 at 2x1R200: Right, a new 2 at 0x0, 16 at 2x2D401: Down, a new 4 at 0x1, 32 at 3x2
L212: Left, a new 2 at 1x2, 4 at 2x2, 64 at 3x1

Add the merge scores together: 4 + 4 + 8 + 4 + 4 + 8 + 16 + 32 + 4 + 64 = 148

Add 148 to the starting score: 200 + 148 = 348

Examples1

  1. Example 1

    Input
    200
    L213D202R211D210D200D212R410R200D401L212
    0 2 0 0 0 4 0 0 2 8 2 0 8 32 16 2
    
    Expected output
    348