コード例 #1
0
  public static void main(String[] args) {

    // create initial board from file
    In in = new In(args[0]);
    int N = in.readInt();
    int[][] tiles = new int[N][N];
    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++) {
        tiles[i][j] = in.readInt();
      }

    Board initial = new Board(tiles);
    /*
    System.out.println(initial.isSolvable());
    System.out.println(initial.toString());
    Iterable<Board> newbrdstack = new Stack<Board>();
    newbrdstack = initial.neighbors();
    System.out.println(newbrdstack.toString());
     */

    // check if puzzle is solvable; if so, solve it and output solution
    if (initial.isSolvable()) {
      Solver solver = new Solver(initial);
      StdOut.println("Minimum number of moves = " + solver.moves());
      for (Board board : solver.solution()) StdOut.println(board);
    }

    // if not, report unsolvable
    else {
      StdOut.println("Unsolvable puzzle");
    }
  }
コード例 #2
0
ファイル: Solver.java プロジェクト: rarry/coursera
  /**
   * solve a slider puzzle (given below)
   *
   * @param args
   */
  public static void main(String[] args) {
    // create initial board from file
    In in = new In(args[0]);
    int N = in.readInt();
    int[][] blocks = new int[N][N];
    for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) blocks[i][j] = in.readInt();
    Board initial = new Board(blocks);

    // solve the puzzle
    Solver solver = new Solver(initial);

    // print solution to standard output
    if (!solver.isSolvable()) StdOut.println("No solution possible");
    else {
      StdOut.println("Minimum number of moves = " + solver.moves());
      for (Board board : solver.solution()) StdOut.println(board);
    }
  }
コード例 #3
0
ファイル: Solver.java プロジェクト: qiaoyuguo/practice
  public static void main(String[] args) // solve a slider puzzle (given below)
      {
    In in = new In(args[0]);
    int N = in.readInt();
    int[][] blocks = new int[N][N];
    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++) {
        blocks[i][j] = in.readInt();
      }

    Board brd = new Board(blocks);
    Solver slv = new Solver(brd);
    Iterable<Board> queue = slv.solution();
    Iterator it = queue.iterator();
    StdOut.printf("Minimum number of moves = %d\n", slv.moves());
    while (it.hasNext()) {
      StdOut.println(it.next());
    }
  }