集册 Java实例教程 八皇后问题

八皇后问题

欢马劈雪     最近更新时间:2020-01-02 10:19:05

618
八皇后问题

class Queen {

  static final int n = 8;
  /*
   from NowJava.com 
  */

  static int[] queen = new int[n]; // position (i,queen[i])

  static int nbsol;


  static void displayChessboard() {

    int i, j;


    System.out.println("");


    for (i = 0; i < n; i++) {

      for (j = 0; j < n; j++) {

        if (queen[i] != j)

          System.out.print("0");

        else

          System.out.print("1");

      }

      System.out.println("");

    }

  }
/** 来 自 nowjava - 时代Java**/

  static boolean wrongPos(int i1, int j1, int i2, int j2) {

    return (i1 == i2 || j1 == j2 || Math.abs(i1 - i2) == Math.abs(j1 - j2));

  }


  // Place safely queen i at column j?

  static boolean safeMove(int i, int j) {

    boolean result = true;


    for (int k = 0; k < i; k++)

      result = result && !wrongPos(i, j, k, queen[k]);


    return result;

  }


  static boolean search(int row) {

    boolean result = false;


    if (row == n) {

      displayChessboard();

      nbsol++;

    } else {

      int j = 0;

      while (!result && j < n) {

        if (safeMove(row, j)) {

          queen[row] = j;

          result = search(row + 1);

        }

        // Backtracking here

        j++; 
展开阅读全文