2048
InterviewTime limit2sMemory limit512 MB
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
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









