Dice Stamp
Time limit8sMemory limit512 MB
Each die traces a fixed path of cells and overwrites each cell with its bottom-face value; choose N button presses, repeats allowed, to maximize the final sum on the board.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Bit manipulation, Simulation, Implementation
- Solved
- No attempts yet
Problem
At a local festival you find a stall running a game you have never seen before. N six-sided dice are dropped onto a board and rolled. More precisely, N buttons are paired one-to-one with the N dice, and pressing a button drops the corresponding die onto the board. You press buttons as you like N times, dropping and rolling the dice N times, and score points.
Here are the detailed rules. All N dice used in the game are cubes with edge length 1, and the board is a sufficiently large plane divided into square cells of side length 1. Before the game starts, every cell on the board has 0 written on it. Each face of each die has an integer written on it. These integers are not necessarily 1 through 6, and different dice may have different numbers.
The machine used in the game has N buttons, paired one-to-one with the N dice. Pressing any button ejects the corresponding die from the machine, drops it onto the board, and it rotates several times. During the rotation, the bottom face of the die always coincides exactly with some cell of the board. Each time the bottom face touches a cell, the number written on that cell is overwritten with the number written on the die's bottom face. This includes the moment the die first touches the board as it falls. After the rotation stops, the die is removed from the board and returned to its ejection device. After you press a button N times, the sum of the numbers written on the board is the final score. You may press the same button multiple times, but you cannot press the next button until the previously ejected die finishes rotating and returns to the ejection device.
The stall owner claims the dice are ejected randomly, but you, being observant, notice while watching other customers play that the behavior of pressing the same button is exactly the same regardless of the earlier button presses. More specifically, pressing the i-th button behaves deterministically as follows.
- The
i-th die is ejected. - This die falls onto a predetermined cell in a predetermined orientation. This orientation is always one where the square of the cell and the square of the bottom face coincide exactly.
- The die repeatedly rotates in one of the four directions: front, back, left, right. The number of rotations and the direction of each rotation are also predetermined.
- When the predetermined rotations finish, the die is removed from the board and returned to the ejection device.
For convenience, consider three-dimensional space, take the x and y axes parallel to the cell's edges, and let the direction the die's top face points be the positive z direction. Then a rotation of the die is one of the four directions: positive or negative x, positive or negative y, as shown in the figure below. The symbols in the figure correspond to the input format described later.

Though you feel cheated that it moves deterministically, you realize you can change the final score by how you press the N buttons.
Through careful observation you have gathered complete information: the numbers on each face of each die, the initial position and orientation it is dropped at, and the way it rotates afterward. Using this information, find the highest score obtainable in this game with the best button-pressing strategy.
Input
The input consists of at most 40 datasets. Each dataset is given in the following format.
N
information for die 1
...
information for die N
The first line of the input contains a single integer N, the number of dice. You may assume 1 ≤ N ≤ 15. Then the information for N dice follows.
Each die's information is given in the following format.
x y
l r f b d u
rot
The first line contains two integers x, y, the coordinates (x, y) of the center of the cell the die is dropped onto when ejected. You may assume -1,000 ≤ x, y ≤ 1,000.
The second line contains six integers l, r, f, b, d, u, the numbers written on each face. l, r, f, b, d, u are the numbers on the faces pointing in the negative x, positive x, negative y, positive y, negative z, and positive z directions respectively when dropped. You may assume 1 ≤ l, r, f, b, d, u ≤ 100.
The third line contains a string rot describing the rotations. rot consists only of the characters 'L', 'R', 'F', 'B', and has length between 1 and 30 inclusive. The j-th character of rot is the direction of the j-th rotation; when the character is 'L', 'R', 'F', 'B', it means rotation in the negative x, positive x, negative y, positive y direction respectively.
The end of the input is indicated by a line containing a single zero.
Output
For each dataset, output the highest score obtainable by choosing the N button presses well, on one line. Each output line must not contain any characters other than this number.