2,147,483,648 Game
InterviewTime limit1sMemory limit512 MB
Given an 8x8 2048-style board and one arrow key, apply the slide-and-merge rules once and print the resulting board.
- Level
Medium4 of 10
- Topics
- Simulation, Implementation, Array, Matrix
- Solved
- No attempts yet
Problem
Hojun is playing the 2,147,483,648 game, an extension of 2048.
The 2,147,483,648 game is played on an (8 \times 8) board. You move tiles that hold integers of the form (2^k) ((1 \le k \le 30)) using the arrow keys, and the goal is to create (2,147,483,648(=2^{31})). When an arrow key is pressed, the game proceeds by the following rules.
- Push all tiles in the direction of the pressed arrow key until they hit the wall.
- If two tiles with the same number are adjacent in the pushing direction, merge those two tiles. If three or more are adjacent, merge them two at a time, starting from the pair closest to the wall the arrow key points toward.
- In a single arrow key input, no tile is merged more than once.
For example, suppose the current board is in the state shown in the following figure.

When the left arrow key (L) is pressed here, the board changes as shown in the following figure.

Hojun has played this game, which does not even have an undo feature, all night for several days, and now he cannot grasp at all how the board will change even after pressing a single key. Write a program that, given the current board and the arrow key to press, prints the board after the rules above are applied.
Input
Eight lines follow, each containing 8 integers separated by spaces. 0 means an empty cell, and every other integer satisfies the form (2^k) ((1≤k≤30)).
The 9th line contains one letter among U, D, L, R. Each means pressing the up, down, left, and right arrow key, respectively.
Output
Print the result of pressing the arrow key on the given board. Print it in the same format as the input board.