// 2005 ACM Mid-Central Regional Programming Contest // Problem E: Time to Graduate // Posed by Wen Hsin, Park University // Written by John Cigas, Rockhurst University // Coded by Andy Harrington, Loyola University, Chicago import java.io.*; import java.util.*; public class graduate { static List courses; static boolean [][] offered; // [courses index][0: fall or 1:spring] static boolean [][] prereq; // [course index][course index of possible prereq] static int N; // number of courses in this data set static int coursePerSem; // maximum number of courses per semester public static void main(String[] args) { Scanner in = null; String file = (args.length > 0) ? args[0] : "graduate.in"; try { in = new Scanner(new BufferedReader( new FileReader(file))); } catch(Exception e) { System.out.println("Can't open input " + file); System.exit(-1); } N = in.nextInt(); while (N > 0) { coursePerSem = in.nextInt(); courses = new ArrayList(N); offered = new boolean[N][2]; prereq = new boolean[N][N]; // list of course names for (int i=0; i(), 0) + "."); N = in.nextInt(); } } // return the number of semesters needed if numSemesters have elapsed // and the specified courses have already been taken. static int findClasses(ArrayList taken, int numSemesters) { if (taken.size() == N) return numSemesters; ArrayList toAdd = new ArrayList(); for (int i=0; i> allComb = new ArrayList>(); combinations(new ArrayList(), Math.min(coursePerSem, toAdd.size()), 0, toAdd, allComb); // Put all legal subsets of toAdd into allComb for (ArrayList c : allComb) { // test branch for each such subset ArrayList tempTaken = new ArrayList(taken); tempTaken.addAll(c); int time = findClasses(tempTaken, numSemesters+1); if (time < minTime) minTime = time; } return minTime; } static boolean havePrereqs(int course, ArrayList taken) { for (int i=0; i= next. static void combinations(ArrayList subsetBase, int tot, int next, ArrayList allElem, ArrayList> allComb) { if (tot == 0) allComb.add(subsetBase); else { if (tot < allElem.size() - next) // make combinations without element at combinations(subsetBase, tot, next+1, allElem, allComb); // next index ArrayList basePlus = new ArrayList(subsetBase); basePlus.add(allElem.get(next)); // make combinations with element at combinations(basePlus, tot-1, next+1, allElem, allComb); // next index } } }