Problem L
Yummy Sun Rays
Mr. P is playing the board game Photosynthesis. In the game, players grow and harvest trees in order to gain points. Growing and harvesting trees requires an in-game currency called Sun Tokens. At the beginning of each round, players gain Sun Tokens depending on the current configuration of the board and the position of the sun.
The board has a fixed octagonal shape consisting of exactly $37$ circular cells, labeled from $1$ to $37$. Each cell may either be empty or contain exactly one tree. There are three types of trees:
-
Small tree (S)
-
Medium tree (M)
-
Large tree (L)
In this problem, the sun is always located at the top-left corner of the board. From this position, sunlight travels diagonally across the board toward the bottom-right. Each diagonal path of sunlight is called a sun ray.
A tree generates Sun Tokens if and only if it is reached by a sun ray and is not blocked by another tree. The number of Sun Tokens generated depends on the size of the tree:
-
Small tree generates $1$ Sun Token
-
Medium tree generates $2$ Sun Tokens
-
Large tree generates $3$ Sun Tokens
Each tree casts a shadow in the same diagonal direction as the sun rays. The length of the shadow depends on the tree size:
-
Small tree casts a shadow of length $1$
-
Medium tree casts a shadow of length $2$
-
Large tree casts a shadow of length $3$
A tree does not generate Sun Tokens if it lies within the shadow of another tree. A shadow can only block a tree of the same height or smaller. In particular:
-
A small tree cannot block a medium or large tree
-
A medium tree cannot block a large tree
If multiple shadows overlap a tree, the blocking rule still applies: the tree is blocked if at least one valid shadow reaches it.
Given a board configuration, calculate how many Sun Tokens Mr. P’s trees generate.
Input
The input describes a single board configuration.
The first line contains an integer $N$ ($0 \le N \le 36$), the number of trees belonging to other players.
The next $N$ lines each contain:
-
an integer $i$ ($1 \le i \le 37$), the board position of the tree, and
-
a character $x \in \{ \texttt{S}, \texttt{M}, \texttt{L}\} $ indicating whether the tree is Small, Medium, or Large.
The following line contains an integer $M$ ($1 \le M \le 37$), the number of trees belonging to Mr. P.
The next $M$ lines each contain:
-
an integer $j$ ($1 \le j \le 37$), the board position of the tree, and
-
a character $y \in \{ \texttt{S}, \texttt{M}, \texttt{L}\} $ indicating whether the tree is Small, Medium, or Large.
It is guaranteed that:
-
$N + M \le 37$, and
-
no two trees occupy the same board position.
Output
Output a single integer: the total number of Sun Tokens generated by Mr. P’s trees.
| Sample Input 1 | Sample Output 1 |
|---|---|
3 12 L 10 M 9 S 3 19 L 17 M 15 S |
0 |
| Sample Input 2 | Sample Output 2 |
|---|---|
3 19 L 17 M 15 S 3 12 L 10 M 9 S |
6 |
| Sample Input 3 | Sample Output 3 |
|---|---|
0 6 12 L 10 M 9 S 19 L 17 M 15 S |
6 |
