Rolling Cubes
InterviewTime limit8sMemory limit512 MB
Simulate rolling a 1x2x4 block on a grid and report its new corner position plus the colors on its front, left, and top faces.
- Level
Medium4 of 10
- Topics
- Simulation, Implementation, Array, Math
- Solved
- No attempts yet
Problem
A wooden block stands on a large board ruled into one-inch squares. The block is one inch deep, two inches wide, and four inches tall. At first the block sits on exactly two squares. Its top face is black, its bottom face is white, its front face is green, its back face is red, its left face is blue, and its right face is yellow.
The block is then moved to a new location on the board by rolling. Throughout the rolling, one edge of the block stays in contact with a line of the board. The block never slides.
Four basic operations can be performed on the block: F (rolling the block forward), B (backwards), L (to the left), and R (to the right).

Figure 1: The rolling cubes
The diagram shows the original position of the block (center) and how it looks after each of the four basic operations, though not its exact location on the board.
Given a sequence of basic operations, write a program that outputs the resulting state of the block.
Input
The input contains a series of data sets. Each data set is a single line holding a string of up to 1000 letters drawn from ‘F’, ‘B’, ‘L’, and ‘R’.
Output
For each data set, follow the given sequence of basic operations, then print the new location of the front left corner of the block relative to the original location and the colors on the front, left, and top of the block. The output must follow the sample output below.