Flipped Course
View as PDFPhoebe volunteered to navigate the kayaking event because, in her words, "a map is just a very small grid." After arriving nowhere near the finish line, she admits that she held the map upside down for exactly one nonempty consecutive section of the course. She cannot remember when she turned it over, but she is quite sure this explains everything.
The kayak starts at coordinates . The intended course consists of
moves, each one
metre north, south, east, or west. During the section when the map was upside down, each move
was taken in the opposite direction: north became south, south became north, east became west,
and west became east. All moves outside that section were taken correctly, and the moves were
always performed in their original order.
Given the intended course and the kayak's actual finishing coordinates , count how many
sections could explain the result. A section is identified by its first and last move, so two
sections are different if either endpoint differs. If no section could produce the given finish,
output
0.
Input
The first line contains an integer (
), the number of moves.
The second line contains a string of length
, describing the intended moves in order:
Nincreases the-coordinate by
.
Sdecreases the-coordinate by
.
Eincreases the-coordinate by
.
Wdecreases the-coordinate by
.
The third line contains two integers and
(
), the actual finishing
coordinates.
Output
Print a single integer: the number of nonempty consecutive sections for which taking every move
in the opposite direction would make the kayak finish at .
Example 1
Input
4
NSNS
0 0
Output
4
Explanation
The possible sections are moves to
,
to
,
to
, and
to
.
Each has zero net displacement, so reversing its directions leaves the finish unchanged.
The empty section is not allowed and is not counted.
Example 2
Input
1
E
-1 0
Output
1
Explanation
The only nonempty section is the entire course. Reversing its one move takes the kayak west instead of east.
Example 3
Input
2
NE
0 0
Output
0
Explanation
Reversing only the first move finishes at , reversing only the second
finishes at
, and reversing both finishes at
. None reaches
.
Comments