North and South

Problem Description

The problem is to infer the geographic relationship between two cities, given a set of facts about the geographic relationship between cities. The input to the program is divided into two sets of data: the facts and the queries. The first set defines a geography relationship between cities, and the second set is a collection of questions about this relationship. Each data set consists of pairs of distinct city names.

In the first data set, a line

city1  city2
means that the first city of the pair is south of the second city.

In response to each pair of cities in the second data set, the program is to print one of the following lines of output:

city1 is south of city2.
city1 is north of city2.
The relative position of city1 and city2 is unknown.
The correct response depends upon all the facts established by the first data set.

The relation "is south of" is a transitive relationship, so if city1 is south of city2 and city2 is south of city3, then city1 is south of city3. You may assume that the input is consistent, that is, there will be no information that implies a city is south of itself.

Sample Input and Output

For the following input:
Melbourne    DaytonaBeach
DaytonaBeach Jacksonville
Orlando   Jacksonville
#
Melbourne Jacksonville
Tampa Melbourne
Jacksonville Orlando
Orlando DaytonaBeach
#
the output should be
Melbourne is south of Jacksonville.
The relative position of Tampa and Melbourne is unknown.
Jacksonville is north of Orlando.
The relative position of Orlando and DaytonaBeach is unknown.
In the input, the city pairs will be on the same line and separated by a least one space. You may assume that there are no leading and trailing spaces. The names of the cities consist only of upper and lower-case letters [a-zA-Z]. Capitialization of the names is significant. That is, DaytonaBeach is considered a different city than Daytonabeach. Each of the two sections of the input is ended by a line containing the single character #.

Files and IO

Write the program in one file named north as appropriate for the language. (North.java, north.cpp, north.hs, north.pas, north.adb, etc.) All input is from the standard input; all output is to the standard output. The program does not read or write any files.

History

This problem originally appeared as "Problem B: North and South" in the 1998 Southeast USA Region Programming Contest at the University of Florida.
Ryan Stansifer <ryan@cs.fit.edu>
Last modified: Fri Jul 30 13:34:17 EDT 2004