Swarm
Time limit10sMemory limit512 MB
Given a set of allowed compass directions, place all bees on one integer point minimizing the total number of moves.
- Level
Medium7 of 10
- Topics
- Geometry, Shortest path, Math, Implementation
- Solved
- No attempts yet
Problem
A violent storm has scattered a swarm of bees across the coordinate plane. Each bee is now at some point with integer coordinates. All the bees want to gather at a single point, which must also have integer coordinates. The bees in this swarm share a genetic mutation, so they can fly only in certain directions. The allowed directions are a subset of the eight compass directions: the four cardinal directions and the four intercardinal directions (northeast, northwest, southeast, southwest). As usual, north is the direction of increasing and east is the direction of increasing .
In one step, one bee can move to a neighboring integer point in one of the allowed directions. For example, one step northeast moves a bee from to , and one step west moves it from to . Find the minimum total number of steps needed for all the bees to end up at the same point.

Illustration of the first example
Input
The first line contains an integer (), the number of allowed directions.
The second line contains distinct strings separated by single spaces. Each string is one of N, NW, W, SW, S, SE, E, NE, which stand for north, northwest, west, southwest, south, southeast, east, and northeast, in that order.
The next line contains an integer (), the number of bees.
The -th of the following lines contains integers and (), the coordinates of the point where the -th bee is now. All bees are at distinct points.
Output
Print the minimum total number of steps. The input guarantees that all the bees can gather at one point.