Joy's Territory
Time limit1sMemory limit256 MB
Count the unit squares with all four corners visited by repeating the same N-step walk for K days from each day's endpoint.
- Level
Medium7 of 10
- Topics
- Geometry, Simulation, Math, Hash map
- Solved
- No attempts yet
Problem
You live in a city shaped like a grid of roads. Many roads run very far in the north to south direction, and many other roads run very far in the east to west direction. The gap between two neighbouring north to south roads is 1 km. The gap between two neighbouring east to west roads is also 1 km.
The city has one city hall. The crossing where the city hall stands is written as . Every crossing of this city is written as crossing with two integers and . Crossing is the crossing reached from crossing by going km east ( km west when ) and km north ( km south when ).
The city hall keeps one dog named Joy. Joy made a walking plan for days. The plan is the following.
- On the morning of the first of the days, Joy is at crossing . Joy leaves a mark on crossing . No crossing other than has a mark from Joy yet.
- Joy walks in the daytime on each of the days. One day of walking consists of steps. In each step Joy moves from the current crossing to a neighbouring crossing and leaves a mark on the crossing it moved to. Joy moves in exactly the same way on every day.
- After the daytime walk ends, Joy sleeps at the crossing it stands on until the next morning.
The city hall talks about the territory Joy builds over the days of walking. If Joy left a mark at least once on all four crossings , , and , then the block enclosed by these four crossings belongs to Joy's territory.
The roads of this city are very long, and there are enough roads in both the north to south direction and the east to west direction, so Joy never reaches the end of a road or the end of the city during a walk.
Given Joy's walking plan, write a program that computes the number of blocks that belong to Joy's territory.
Input
The first line contains two integers and separated by a single space. One day of walking consists of steps, and the walking plan lasts days.
The second line contains a string of length . The -th character () of , counted from the left, is one of E, N, W, S. The characters mean the following.
- If is E, the -th step moves to the crossing neighbouring on the east.
- If is N, the -th step moves to the crossing neighbouring on the north.
- If is W, the -th step moves to the crossing neighbouring on the west.
- If is S, the -th step moves to the crossing neighbouring on the south.
For crossing , the crossings neighbouring on the east, north, west and south are crossing , crossing , crossing and crossing .
Output
Print the number of blocks that belong to Joy's territory on one line.