#P6226. [BalticOI 2019] 潜艇 / Nautilus

[BalticOI 2019] 潜艇 / Nautilus

Description

A submarine is secretly traveling in the ocean.

The ocean is an R×CR \times C grid, where # represents an island and . represents water. The submarine can travel only in water.

Every minute, the submarine sends a radio signal that indicates the direction it will move. Directions are represented by the following letters: North (N), East (E), South (S), West (W).

Now the eavesdropper has set up a radar to intercept the submarine’s signals. During the last MM minutes, the radar collected MM signals, represented by a string of length MM, for example WS?EE??. Some collected signals cannot be decoded and are represented by ?.

The eavesdropper does not know the submarine’s initial position, but wants to use the map to determine the submarine’s current position. Please help compute how many possible current positions the submarine may be in.

Input Format

The first line contains three integers R,C,MR, C, M.

The next RR lines each contain CC characters describing the sea map.

The last line is a string of length MM, representing the intercepted signals. It is guaranteed that the string contains only N, E, S, W, ?.

Output Format

Output one integer: the number of possible current positions of the submarine.

5 9 7
...##....
..#.##..#
..#....##
.##...#..
....#....
WS?EE??
22

Hint

The scores and Constraints for each subtask are as follows:

  • Subtask 1 (29 points): 1R,C,M1001 \leq R, C, M \leq 100, and there is no ? in the intercepted signals.
  • Subtask 2 (37 points): 1R,C,M1001 \leq R, C, M \leq 100.
  • Subtask 3 (34 points): 1R,C5001 \leq R, C \leq 500, 1M50001 \leq M \leq 5000.

Translated by ChatGPT 5