Tilt tile puzzle
Time limit1sMemory limit1024 MB
Simulate k tilt moves on an n by m grid of empty, white, and black tiles, then print the final grid.
- Level
Medium6 of 10
- Topics
- Simulation, Implementation
- Solved
- No attempts yet
Problem
Bajtazar is playing with a puzzle shaped like a rectangle of cells. Each cell is either empty or holds a white or black tile.
In each move, the puzzle is tilted in one of four directions parallel to the sides of the rectangle. All tiles then slide as far as they can in that direction, without leaving the frame and without overlapping each other.
Bajtazar made a certain sequence of moves. Print the state of the puzzle after his last move.
Input
The first line contains the height and the width (). The next lines describe the initial state, row by row from top to bottom. Each line has symbols from left to right. Each symbol is '.' for an empty cell, 'B' for a white tile, or 'C' for a black tile.
The next line contains the number (), the number of moves Bajtazar made. The last line is a string of length describing those moves. The letters are 'G' for up, 'D' for down, 'L' for left, and 'P' for right.
Output
Print the final state of the puzzle in the same format as the initial state: lines, each containing symbols '.', 'B', or 'C'.
Hint
The first move 'G' slides all tiles up, and the state becomes:
.BCC.
...B.
.....
.....
After the second move 'L', the tiles slide left:
BCC..
B....
.....
.....
After the third move 'P', the tiles slide right, which gives the final state.