diff options
author | Camil Staps | 2015-10-20 14:14:52 +0200 |
---|---|---|
committer | Camil Staps | 2015-10-20 14:14:52 +0200 |
commit | da0e70ef00819bc847b44fc99ea84b935a09799c (patch) | |
tree | 4da0755be58039e5391f3b730c4f5bb559484691 /Practical1/src/nl/camilstaps/cs/GarbageCollectionHelper.java | |
parent | Finish assignment 7 (diff) |
First version practical 1 (garbage collection)
Diffstat (limited to 'Practical1/src/nl/camilstaps/cs/GarbageCollectionHelper.java')
-rw-r--r-- | Practical1/src/nl/camilstaps/cs/GarbageCollectionHelper.java | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/Practical1/src/nl/camilstaps/cs/GarbageCollectionHelper.java b/Practical1/src/nl/camilstaps/cs/GarbageCollectionHelper.java new file mode 100644 index 0000000..8367ea2 --- /dev/null +++ b/Practical1/src/nl/camilstaps/cs/GarbageCollectionHelper.java @@ -0,0 +1,57 @@ +package nl.camilstaps.cs; + +import nl.camilstaps.cs.graphs.Graph; +import nl.camilstaps.cs.graphs.Node; + +import java.util.Scanner; + +/** + * Solution for the Garbage Collection problem. + * + * @author Camil Staps + */ +class GarbageCollectionHelper { + + /** + * Read in a Graph from stdin and print whether we can place N bins to stdout + * @param args + */ + public static void main(String[] args) { + GarbageCollectionMap graph = new GarbageCollectionMap(); + int n_bins = readGraph(new Scanner(System.in), graph); + System.out.println(graph.canPlaceNBins(n_bins) ? "possible" : "impossible"); + } + + /** + * Read a Graph in the defined format: + * + * [n_edges] [n_nodes] [n_bins] + * [fst] [snd] + * [fst] [snd] + * [fst] [snd] + * ... + * + * @param sc the Scanner to use + * @param graph the Graph to build + * @return the number of bins + */ + private static int readGraph(Scanner sc, Graph graph) { + int n_edges = sc.nextInt(); + int n_nodes = sc.nextInt(); + int n_bins = sc.nextInt(); + + for (int i = 1; i <= n_nodes; i++) { + graph.addNode(new Node(i)); + } + + for (int i = 0; i < n_edges; i++) { + int fst = sc.nextInt(); + int snd = sc.nextInt(); + graph.getNode(fst - 1).getNeighbours().add(graph.getNode(snd - 1)); + graph.getNode(snd - 1).getNeighbours().add(graph.getNode(fst - 1)); + } + + return n_bins; + } + +} |