eolymp
bolt
Try our new interface for solving problems
Məsələlər

Erratic Ants

Erratic Ants

Zaman məhdudiyyəti 1 saniyə
Yaddaşı istafadə məhdudiyyəti 64 MiB

The ants of a particular colony are in search of food. Unfortunately hidden dangers are all around the colony which makes foraging difficult. There are traps, obstacles, and predators lurking about. Fortunately, the colony has the perfect ant for the job. Max is neither a smart ant nor an efficient ant but he has got blind luck on his side. In all of his wanderings, he has always managed to stay on safe ground and he (eventually) always finds a source of food to report back to the colony.

The problemi st hat Max rarely takes anything resembling an optimal (shortest) route to find a food source. However, Max can reliably bring back the exact details of the (often winding and convoluted) path that he took to get to the food source. Your job is to help the colony by finding the optimal route located within Max’s convoluted directions to allow the colony toforage more efficiently.

Giriş verilənləri

The first integer in the input, 1n100, denotes the number of paths to food that Max has reported back. This is followed by a blank line and then descriptions of each of the n paths. Each path description begins with an integer, s (0s60), which denotes the number of steps taken in the path. The next s lines contain directional steps expressed as upper-case characters N, E, S, and W corresponding to steps taken in the directions north, east, south, and west respectively. Each step moves Max one unit of distance. Max’s paths always start at the colony and end at a food source. Between each pair of path descriptions is a blank line.

When searching for an optimal path, the only directional steps that may be taken are ones that have previously been taken by Max, or the same steps in reverse.

Çıxış verilənləri

For each given path, give the number of steps found to be in an optimal (shortest) path.

Nümunə

Giriş verilənləri #1
3

8
S
E
E
E
N
W
S
S

4
S
E
N
W

3
S
E
N
Çıxış verilənləri #1
4
0
3
Mənbə 2013 ACM-ICPC North American Qualification Contest