eolymp
bolt
Try our new interface for solving problems
Problems

Sticker Collector Robot

Sticker Collector Robot

One of the favorite sports in RoboLand is the Robots Rally. This rally is practiced in a giant rectangular arena of square cells with dimensions \textbf{N} rows by \textbf{M} columns. Some cells are empty, some contain a sticker for the World Footbal Cup Album (much appreciated by artificial intelligences in RoboLand) and some are occupied by pillars which support the roof of the arena. During the rally the robots can occupy any cell in the arena, except those containing pillars, since they block the robot movement. The path of the robot in the arena during the rally is determined by a sequence of instructions. Each instruction is represented by one of the following characters: '\textbf{D}', '\textbf{E}' and '\textbf{F}', meaning, respectively, "\textbf{turn 90 degrees to the right}", "\textbf{turn 90 degrees to the left}" and "\textbf{move forward one cell}". Robots start the rally in some initial position in the arena and follow closely the sequence of instructions given (after all, they are robots!). Whenever a robot occupies a cell that contains a Cup sticker he collects it. Stickers are not replaced, that is, each scticker can be collected only once. When a robot tries to move into a cell which contains a pillar he stalls, remaining in the cell where he was, with the same orientation. The same happens when a robot tries to leave the arena. Given the map of the arena, describing the position of pillars and sctickers, and the sequence of instructions of a robot, you must write a program to determine the number of stickers collected by the robot. \InputFile The input contains several test cases. The first line of a test case contains three integers \textbf{N}, \textbf{M} and \textbf{S} (\textbf{1} ≤ \textbf{N}, \textbf{M }≤ \textbf{100}, \textbf{1} ≤ \textbf{S} ≤ \textbf{5}×\textbf{10^4}), separated by white spaces, indicating respectively the number of rows, the number of columns of the arena and the number of instructions to the robot. Each of the following \textbf{N} lines describes a cell line of the arena and contains a string with \textbf{M} characters. The first line to appear in the description of the arena is the one more to the North, the first column to appear in description of a cell line of the arena is the one more to the West. Each cell in the arena is described by one of the following characters: \begin{itemize} \item '\textbf{.}' - normal cell; \item '\textbf{*}' - cell which contains a sticker; \item '\textbf{#}' - cell which contains a pillar; \item '\textbf{N}', '\textbf{S}', '\textbf{L}', '\textbf{O}' - cell where the robot starts the rally (only one in the arena). The letter represents the initial robot orientation (North, South, East and West, respectively). \end{itemize} The last line in the input contains a sequence of S characters among '\textbf{D}', '\textbf{E}' and '\textbf{F}', representing the instructions to the robot. The last test case is followed by a line which contains only three numbers zero separated by a blank space. \OutputFile For each rally described in the input your program must print a single line containing a single integer indicating the number of stickers that the robot collected during the rally.
Time limit 1 second
Memory limit 64 MiB
Input example #1
3 3 2
***
*N*
***
DE
4 4 5
...#
*#O.
*.*.
*.#.
FFEFF
10 10 20
....*.....
.......*..
.....*....
..*.#.....
...#N.*..*
...*......
..........
..........
..........
..........
FDFFFFFFEEFFFFFFEFDF
0 0 0
Output example #1
0
1
3
Source Maratona de Programa¸c˜ao da SBC – ACM ICPC – 2010