/* Subject: Contest submission for problem #2, file 2.java */ /* jeffpast@imail.EECS.Berkeley.EDU */ /* Thu Nov 20 12:11:57 PST 2003 */ /*___CONTEST_SUBMISSION___ jeffpast 2 */ package hw9; import java.io.*; import java.util.*; class Shopping { public static void main(String[] args) throws IOException { Graph graph = new Graph(); StreamTokenizer t = new java.io.StreamTokenizer(new InputStreamReader(System.in)); t.nextToken(); int start = (int)t.nval; t.nextToken(); int sought = (int)t.nval; t.nextToken(); while (t.nval >= 0) { Node nextNode = graph.AddNode((int)t.nval); t.nextToken(); nextNode.itag1 = (int)t.nval; t.nextToken(); } t.nextToken(); t.nextToken(); while (t.ttype != t.TT_EOF && t.ttype != '=') { int p1 = (int)t.nval; t.nextToken(); int p2 = (int)t.nval; graph.AddEdge( graph.GetNodeFromID(p1), graph.GetNodeFromID(p2),1); } Node startNode = graph.GetNodeFromID(start); LinkedList result = FindPlaces(startNode, sought); int nextNumber = -1; System.out.print ("Starting at " + start + ", you might first encounter type " + sought + " shop #"); nextNumber = GetSmallest(result); if (nextNumber == -1) System.exit(0); System.out.print (nextNumber); while ( (nextNumber = GetSmallest(result)) != -1) { System.out.print (", " + nextNumber); } System.exit(0); } public static int GetSmallest(LinkedList list) { int minID = -1; int minElement = -1; int minAmount=9999999; for (int i = 0; i < list.size(); i++) { Node nextNode = (Node)list.get(i); if (nextNode.id < minAmount) { minID = nextNode.id; minElement = i; minAmount = nextNode.id; } } list.remove(minElement); return minID; } public static LinkedList FindPlaces (Node start, int sought) { LinkedList result = new LinkedList(); if (start.itag2 == 1) return result; start.itag2 = 1; if (start.itag1 == sought) { result.add(start); return result; } LinkedList neighbors = start.Neighbors(); Iterator i = neighbors.listIterator(); while (i.hasNext()) { Node nextNode = (Node)i.next(); result.addAll(FindPlaces(nextNode,sought)); } return result; } } class Graph { public LinkedList nodes = new LinkedList(); //public LinkedList edges = new LinkedList(); public Node AddNode (int id) { Node node = new Node(); node.id = id; nodes.add(node); return node; } public Node GetNode(int index){ return (Node)nodes.get(index); } public Node GetNodeFromID(int id) { for (int i = 0; i < nodes.size(); i++) { Node nextNode = GetNode(i); if (nextNode.id == id) return nextNode; } return null; } public void AddEdge(Node node1, Node node2, int weight) { node1.AddEdge(node2,weight); node2.AddEdge(node1,weight); } } class Node { public int id; public Object tag1=null; public Object tag2=null; public int itag1=0; public int itag2=0; public int itag3=0; public int itag4=0; public LinkedList edges = new LinkedList(); public Edge AddEdge (Node dest, int weight) { Edge edge = new Edge(); edge.weight = weight; edge.node = dest; this.edges.add(edge); return edge; } public LinkedList Neighbors (){ LinkedList result = new LinkedList(); ListIterator i = edges.listIterator(); while (i.hasNext()) { Edge nextEdge = (Edge)i.next(); result.add(nextEdge.node); } return result; } } class Edge { public int weight = 1; public Node node; //public Node node2; }