예제 #1
0
 private Grid solve(Grid grid) {
   if (grid.getRemainingSquares() == 0 && grid.getRemainingCandidates() == 0) {
     return grid;
   }
   Set<Move> moves = grid.findCandidateMoves();
   if (moves.isEmpty()) {
     return null;
   }
   Move bestMove = moves.iterator().next();
   if (bestMove.remainingSquares == 0 && bestMove.remainingCandidates == 0) {
     grid.printCandidates();
     System.out.println("\n" + bestMove + "\n");
     bestMove.executeMove(grid);
     return grid;
   }
   for (Move move : moves) {
     System.out.println("\n" + move + "\n");
     Grid clone = new Grid(grid);
     move.executeMove(clone);
     Grid result = clone.solve();
     if (result != null) {
       return result;
     }
   }
   return null;
 }
예제 #2
0
  public static void main(String[] args) throws IOException {
    /*
    grid.setValues(0, new int [] {0, 0, 0,  0, 0, 0,  0, 0, 0});
    grid.setValues(1, new int [] {0, 0, 0,  0, 0, 0,  0, 0, 0});
    grid.setValues(2, new int [] {0, 0, 0,  0, 0, 0,  0, 0, 0});

    grid.setValues(3, new int [] {0, 0, 0,  0, 0, 0,  0, 0, 0});
    grid.setValues(4, new int [] {0, 0, 0,  0, 0, 0,  0, 0, 0});
    grid.setValues(5, new int [] {0, 0, 0,  0, 0, 0,  0, 0, 0});

    grid.setValues(6, new int [] {0, 0, 0,  0, 0, 0,  0, 0, 0});
    grid.setValues(7, new int [] {0, 0, 0,  0, 0, 0,  0, 0, 0});
    grid.setValues(8, new int [] {0, 0, 0,  0, 0, 0,  0, 0, 0});
    */

    final StringBuilder buf = new StringBuilder();
    FileUtil.readFile(
        "puzzle.txt",
        new LineVisitor() {

          @Override
          public void processLine(String line) throws IOException {
            buf.append(line + "\n");
          }
        });

    Grid grid = new Grid(buf.toString());

    printGrid(grid);
    // grid.printCandidateMoves();
    // System.out.println();
    // Grid clone = new Grid(grid);
    // printGrid(clone);
    printGrid(grid.solve());
  }