P3326 [SDOI2015] 3D Diagram

Description

Xiaoyuan (pinyin) is a smart kid who often explains things he finds interesting to the children around him. Recently, he plans to explain stereograms under colored horizontal light sources to the kids and asks you to help draw them on a plane. Xiaoyuan has a rectangular area of size $m\times n$ with $m\times n$ unit squares, each of side length $1$. On each square, some identical blocks are stacked (each block has length, width, and height all equal to $1$). For convenience, assume this area faces south with north at the top. Below is an example. ![](https://cdn.luogu.com.cn/upload/pic/22187.png) Xiaoyuan wants you to print the stereogram of these squares. We define each block in the following fixed format, and it will never be flipped or rotated; it is always placed strictly in this single orientation (the left side shows the pattern to be printed, and the right side shows the decimal ASCII for each position; ASCII $32$ is a space). ![](https://cdn.luogu.com.cn/upload/pic/22188.png) Under good optical conditions, Xiaoyuan shines $T$ beams of parallel light on these blocks simultaneously. These parallel lights satisfy: each is one of the three primary colors red, green, or blue; the angles with the $x$-axis and $y$-axis are multiples of $45^\circ$; and the angle with the positive $z$-axis is either $45^\circ$, $0^\circ$, or $315^\circ$. Specifically, we consider at most $9$ different directions of parallel light. Their incident directions can be described as: northwest with $45^\circ$ elevation, due north with $45^\circ$ elevation, northeast with $45^\circ$ elevation, due west with $45^\circ$ elevation, vertical incidence from above, due east with $45^\circ$ elevation, southwest with $45^\circ$ elevation, due south with $45^\circ$ elevation, and southeast with $45^\circ$ elevation. For each unit block, the three printable faces are divided into $12$ small triangles. ![](https://cdn.luogu.com.cn/upload/pic/22189.png) The three primary colors red, green, and blue are represented by the letters R, G, and B. The three secondary colors after double overlap, cyan, yellow, and purple, are represented by C, Y, and P, respectively. For triple overlap, i.e., white, use W.

Input Format

The first line contains two positive integers $m,n$, meaning the area has $m$ rows and $n$ columns. Then $m$ lines follow, describing each row from far to near in order. Each line gives $n$ positive integers, indicating how many blocks are stacked at cell $(i,j)$. Then there are $3$ lines, each with three characters, describing the colors of light in the $9$ corresponding directions (same directional order as described on the map). Each character is either one of RGB, indicating the corresponding color, or `*`, indicating no incident light. The nine directions, in order, are: northwest with $45^\circ$ elevation, due north with $45^\circ$ elevation, northeast with $45^\circ$ elevation, due west with $45^\circ$ elevation, vertical incidence from above, due east with $45^\circ$ elevation, southwest with $45^\circ$ elevation, due south with $45^\circ$ elevation, and southeast with $45^\circ$ elevation.

Output Format

Output the rendered result. The output must not contain a leading blank line, and there must be no extra blank line at the end. The first column must not be all spaces, and there must be no trailing spaces at the end of any line.

Explanation/Hint

![](https://cdn.luogu.com.cn/upload/pic/22190.png) For $15\%$ of the testdata, $1\leq n,m\leq 100$, and there is no incident light. For $40\%$ of the testdata, $1\leq n,m\leq 100$, there is only one incident beam, and its direction is guaranteed to be southeast. For $100\%$ of the testdata, $1\leq n,m\leq 100$, the number of blocks stacked at each position does not exceed $100$, the incident light color may be any one of R, G, or B, and there can be at most $9$ incident beams. Translated by ChatGPT 5