Who owns the Amiga?
Time limit1sMemory limit128 MB
Given constraint lines over five rooms, decide whether the Amiga owner is uniquely determined and print that student, or state it cannot be found.
- Level
Medium4 of 10
- Topics
- Brute force, Implementation, Backtracking, Simulation
- Solved
- No attempts yet
Problem
In a corridor of a student dormitory there are five rooms numbered 1, 2, 3, 4 and 5; room 1 is the left-most room. Each room has a door of a different colour — blue, green, red, white and yellow — but not necessarily in that order.

In these rooms live five students — Anna, Bernhard, Chris, David and Ellen — of five different nationalities — Danish, Finnish, Icelandic, Norwegian and Swedish. (Both the names and the nationalities are listed in alphabetical order, so it does not follow that Anna is Danish.)
Each student owns one computer, and the computers are of five different kinds — Amiga, Atari, Linux, Mac and Windows (again in alphabetical order). Each student also has a favourite programming language — C, C++, Java, Pascal and Perl (also alphabetical).
Given some facts about the students, determine who owns the Amiga computer.
Input
The first line contains an integer (), the number of scenarios.
Each scenario begins with a line containing an integer (), the number of fact lines that follow. Each fact line contains three words separated by one or more spaces.
The first and third word are each one of the following (all lower-case, no capital letters):
1 2 3 4 5
blue green red white yellow
anna bernhard chris david ellen
danish finnish icelandic norwegian swedish
amiga atari linux mac windows
c c++ java pascal perl
The second word is a relationship, one of:
same-as left-of right-of next-to
- same-as — the first and third words describe the same room. For example,
blue same-as bernhardsays Bernhard lives in the room with the blue door. - left-of — the first word's room is immediately to the left of the third word's room. For example,
chris left-of perlsays Chris lives in the room immediately to the left of the Perl programmer. - right-of — the first word's room is immediately to the right of the third word's room.
- next-to — the two words describe rooms next to each other. For example,
swedish next-to linuxsays the Swedish student lives immediately to the left or right of the Linux owner.
The input for each scenario is consistent: there is always at least one arrangement that satisfies all of its facts.
Output
For each scenario, print one line beginning with
scenario #n:
where n is the scenario number (starting at 1). If the facts determine a unique owner of the Amiga, continue the line with
xxxx owns the amiga.
where xxxx is that student's name (anna, bernhard, chris, david or ellen). If the owner cannot be determined uniquely, instead print
cannot identify the amiga owner.