Chessboard Dance
InterviewTime limit1sMemory limit128 MB
Simulate a beetle on an 8x8 board following move and turn commands, pushing chains of pieces that may fall off the board.
- Level
Medium5 of 10
- Topics
- Simulation, Implementation, Array, Matrix
- Solved
- No attempts yet
Problem
Another boring Friday afternoon, Betty the Beetle wonders how to amuse herself. She leaves her hiding place to take a walk around the living room of the Bennett house. Mr. and Mrs. Bennett are out at the theatre, and there is a chessboard on the table! "The perfect time to practice my chessboard dance," Betty thinks. She gets so excited that she does not notice some pieces are still on the board, and she begins her practice session! She follows a script that tells her how to move across the chessboard. The script is a sequence such as the one below.

At every moment Betty stands on one square of the chessboard, facing one of four directions (up, down, left, right) as seen from above. Executing a move n instruction, she moves n squares forward in her current direction. If moving n squares would take her off the board, she stops on the last square of the board and does not leave it. There are three kinds of turns — turn right, turn left, and turn back — which change Betty's direction. Turning does not change Betty's position.
If Betty runs into a chess piece while moving, she pushes that piece along with every piece lined up behind it (a tough beetle she is!). This may push some pieces off the edge of the chessboard, but she does not care! In the figure below, the left board shows the initial state and the right board shows the state after the script above has been performed. Upper-case letters denote white pieces and lower-case letters denote black pieces. The arrow marks Betty's position and direction. Note that during the very first move, the black king (r) falls off the right edge of the board!

Write a program that reads the initial state of the board together with the practice script, and outputs the final state of the board after the practice.
Input
The input contains multiple test cases. Each test case has two parts: the initial state of the board and the script.
The board is given as eight lines of eight characters. The letters r, d, t, a, c, p denote black pieces, the letters R, D, T, A, C, P denote white pieces, and a period (.) denotes an empty square. The square where Betty starts is marked by one of the four characters <, >, ^, v, which also gives her initial direction (left, right, up, and down respectively). The board is not necessarily a legal chess position.
The script comes immediately after the board. It consists of several lines (between 0 and 1000). Each line holds exactly one instruction in one of the following formats, where n is a non-negative integer:
move n
turn left
turn right
turn back
Each test case ends with a line containing a single # character. The final line of the input contains two dash characters (--).
Output
For each test case, print the final state of the board in the same 8×8 format as the input. Print a blank line between the boards of consecutive test cases.