Letters
Time limit2sMemory limit256 MB
Given a grid of letters, apply a sequence of gravity directions (left, right, up, down) where letters slide until blocked, and print the final grid.
- Level
Easy3 of 10
- Topics
- Matrix, Simulation, Implementation
- Solved
- No attempts yet
Problem
Martin is attending a lecture on linear algebra. Needless to say, the professor giving the lecture is the most boring person in the entire universe.
There is an matrix written on the blackboard. Some entries are letters of the English alphabet, and the rest are blank. Here is an example of a matrix:
Martin has no idea what the matrix represents. He has not followed the lecture for the last 30 minutes. Instead, he imagines that gravity suddenly acts on the matrix, and every letter slides downward until it either reaches the bottom or hits the letter below it. After the first phase, the matrix becomes:
Then gravity changes direction and pulls the letters to the left. In this second phase, every letter slides left until it either reaches the left edge or hits the letter on its left. The matrix becomes:
Martin continues this procedure in his head until the lecture ends. After each phase, once all letters have landed, gravity may change direction. The four possible directions are left, right, up, and down.
Write a program that finds the final position of every letter after the given sequence of gravity changes.
Input
The first line contains three integers , , and , where is the size of the matrix and is the number of phases.
The second line contains a string of length made of the letters L, R, U, and D. They stand for the gravity direction in each phase: left, right, up, and down.
The next lines describe the matrix. Each line has characters, each either a lowercase English letter or '.' (a dot), which marks a blank entry.
Output
Print the matrix after the last phase, in the same format as the input.
Constraints