예제 #1
0
파일: SolvePuzzle.java 프로젝트: Drin/Brain
  void getInfo() {
    Scanner console = new Scanner(System.in);
    int maxCost = 0, maxSols = 0;
    Problem prob = null;
    Solver solve = null;
    ArrStack stk = new ArrStack();

    System.out.print("Enter problem type, solution type, " + "max cost and max # of solutions: ");
    try {
      prob = (Problem) Class.forName(console.next()).newInstance();
      solve = (Solver) Class.forName(console.next()).newInstance();
      maxCost = console.nextInt();
      maxSols = console.nextInt();
    } catch (Exception e) {
      System.out.println("" + e);
    }

    try {
      prob.read(console);
    } catch (Exception e) {
      System.out.println("Read error: " + e);
      return;
    }

    Solver.Solution[] sols;
    sols = solve.solveProblem(prob, maxCost, maxSols);
    if (sols == null) {
      System.out.println("No solutions ");
      return;
    }
    System.out.println("Answers are: ");
    for (int ans = 0; ans < sols.length && sols[ans] != null; ans++) {
      System.out.println("Answer " + ans + " with cost " + (sols[ans].mSteps.length - 1));
      for (int stepNdx = 1; stepNdx < sols[ans].mSteps.length; stepNdx++)
        System.out.println("   " + sols[ans].mSteps[stepNdx]);
    }
  }