#P5102. Territory
Territory
Territory
This problem is translated from JOI 2016 Final T4 "Territory".
There is a plane with a rectangular coordinate system. JOI-kun starts at \((0,0)\). Every day he follows a fixed procedure described by a string \(S\) of length \(N\). The string contains only the characters E, N, W, S
. The \(i\)-th character (1-indexed) \(C_i\) represents the direction of his move when he is at point \((x,y)\) before step \(i\):
- \(C_i = \texttt{E}\): move to \((x+1, y)\).
- \(C_i = \texttt{N}\): move to \((x, y+1)\).
- \(C_i = \texttt{W}\): move to \((x-1, y)\).
- \(C_i = \texttt{S}\): move to \((x, y-1)\).
JOI-kun marks the point \((0,0)\) at the start (before making any moves) and also marks the point after finishing each step. On the next day, he starts his procedure from the point where he ended the day before.
After \(K\) days, for any integers \(a, b\), if all four points \((a,b)\), \((a+1,b)\), \((a,b+1)\), \((a+1,b+1)\) have been marked at least once, then the \(1\times 1\) square with these points as vertices is considered part of JOI-kun's territory.
Your task is to compute the number of territories (squares) after \(K\) days.
inputFormat
The input consists of two lines.
- The first line contains two integers \(N\) and \(K\) separated by a space.
- The second line contains a string \(S\) of length \(N\), which consists only of the characters
E, N, W, S
.
outputFormat
Output a single integer: the number of \(1\times 1\) squares that form JOI-kun's territory after \(K\) days.
sample
4 2
ENWS
1
</p>