// Sample solution to Amiga problem // Stein Norheim 2004 // import java.io.*; import java.util.*; class InputSrc { StreamTokenizer tokens = null; public int getInteger() throws IOException { tokens.nextToken(); return Integer.parseInt(tokens.sval); } public String getString() throws IOException { tokens.nextToken(); return tokens.sval; } public InputSrc () throws FileNotFoundException { BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); // new BufferedReader(new FileReader("amiga2.in")); tokens = new StreamTokenizer(stdin); tokens.resetSyntax(); tokens.whitespaceChars(0,32); tokens.wordChars(33,255); } } class Node { public int Id; public String Name; ArrayList sameAs = new ArrayList(); ArrayList rightOf = new ArrayList(); ArrayList leftOf = new ArrayList(); ArrayList nextTo = new ArrayList(); public Node(int id, String name) { Id = id; Name = name; } public void SetSameAs(Node otherNode) { if (!sameAs.contains(otherNode)) { sameAs.add(otherNode); otherNode.SetSameAs(this); } } public void SetLeftOf(Node otherNode) { if (!leftOf.contains(otherNode)) { leftOf.add(otherNode); otherNode.SetRightOf(this); } } public void SetRightOf(Node otherNode) { if (!rightOf.contains(otherNode)) { rightOf.add(otherNode); otherNode.SetLeftOf(this); } } public void SetNextTo(Node otherNode) { if (!nextTo.contains(otherNode)) { nextTo.add(otherNode); otherNode.SetNextTo(this); } } } class Relations { Node[] nodes; String[] namedConstants = { "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"}; Map nameMap; public Relations() { nameMap = new HashMap(); nodes = new Node[namedConstants.length]; for (int i=0; i0) { for (int cand=10; cand<15; cand++) { if (solution[cand]==amigaRoom) return cand; } } return -1; } public void BackTrack(int base, amiga app) { int newOwner = GetAmigaOwner(app); if (base0 && app.amigaOwner==newOwner) return; for (int j=0; j<120 && app.amigaOwner != -1; j++) { boolean mightwork = true; for (int k=0; k<5; k++) { if (!(solution[base+k]==0 || solution[base+k]==app.permutations[j][k])) { mightwork = false; break; } } if (mightwork) { SolutionCandidate testSolution = new SolutionCandidate(this); boolean conflict = false; queue.clear(); for (int k=0; k<5; k++) { if (testSolution.SetFact(app.rel.nodes[base+k],app.permutations[j][k])) { conflict=true; break; } } if (!conflict) conflict = testSolution.BFS(); if (!conflict) testSolution.BackTrack(base+5, app); } } } else { if (newOwner >0) { if (app.amigaOwner==-2 || app.amigaOwner == newOwner) app.amigaOwner=newOwner; else app.amigaOwner=-1; } } } public boolean SetFact(Node node, int position) { if (solution[node.Id] == 0) { solution[node.Id]=position; queue.addLast(new Fact(node,position)); } else { if (solution[node.Id] != position) return true; } return false; } public boolean BFS() { while (!queue.isEmpty()) { Fact currentFact = (Fact) queue.getFirst(); queue.removeFirst(); int i; Node node = currentFact.FactNode; for (i=0; i 0) { System.out.println(rel.nodes[amigaOwner].Name+" owns the amiga."); } else { System.out.println("cannot identify the amiga owner."); } } } public static void main(String[] args) throws IOException{ amiga a = new amiga(); a.realMain(); } }