More Fun in Bicol
Time limit5sMemory limit128 MB
Given unit compass offsets between named places, answer each query with the direction from one place to the other or report it as unknown.
- Level
Medium4 of 10
- Topics
- Union-find, Graph
- Solved
- No attempts yet
Problem
The Bicol region (Region 5) has six provinces: Albay, Camarines Norte, Camarines Sur, Sorsogon, and the island provinces of Catanduanes and Masbate. Its cities are Iriga City, Legazpi City, Ligao City, Masbate City, Naga City, Sorsogon City, and Tabaco City.

Figure H-1: map of the Bicol region (the positions are drawn for illustration only)
Most of the contestants could not wait for the competition to end and started planning trips around the region. They keep asking each other where a place is, and they are writing down the relative location of one place to another.
Every place sits at a point on the plane, with east along and north along . Each of the eight relative positions is one unit step.
Saying that place is north of place means the coordinate of minus the coordinate of equals . A relative position counts one unit on every axis it names, so northeast is one north and one east, and two pairs of places joined by the same relative position are the same distance apart.
Offsets add along a chain. If is northeast of and is west of , then the coordinate of minus the coordinate of is , so is north of . Answer every query from the signs of the accumulated offset.
Input
The input has several test cases, and each test case holds two data sets. The first data set describes the relative location of one place to another. The second data set asks queries on the relative location of one place to another.
The first line of a test case has two integers and , the number of entries in the first and the second data set, where and .
The next lines are position descriptions written as place 1,place 2,relative position. Such an entry says that place 1 is that relative position of place 2. The reverse entry never appears, so an input that carries (Naga City, City of Iriga, Northwest) never carries (City of Iriga, Naga City, Southeast) as well.
The next lines are queries written as place 1,place 2, and each one asks for the relative location of place 1 to place 2.
The fields are comma delimited, may carry spaces around them, and may mix upper and lower case. Two names mean the same place when they match once the surrounding spaces are dropped and case is ignored. A relative position is one of north, south, east, west, northeast, northwest, southeast, southwest. The entries of one test case never contradict each other.
A blank line separates two test cases, and a line with the single integer 0 follows the last test case.
Output
For each test case, print Case k on a line of its own first, where counts the test cases from 1. Then print one line per query, in the order the queries appear.
When the entries connect the two queried places and the accumulated offset is not , print <place 1> is <relative position> of <place 2>. Write both names the way the query writes them once the surrounding spaces are dropped, and write the relative position in lower case.
When the entries carry too little information to connect the two places, or when they put both names on the same point so that none of the eight relative positions fits, print Relative location cannot be determined instead.
The output has no blank lines.