1. 程式人生 > 程式設計 >如何基於java語言實現八皇后問題

如何基於java語言實現八皇后問題

這篇文章主要介紹瞭如何基於java語言實現八皇后問題,文中通過示例程式碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

八皇后問題,在一個8X8的棋盤中,放置八個棋子,每個棋子的上下左右,左上左下,右上右下方向上不得有其他棋子。正確答案為92中,接下來用java語言實現。

程式碼如下

package eightQuen;

/**
 * 八皇后問題
 * 
 * @author 83771
 *
 */
public class eight {
  // 定義一個數組 表示棋盤
  public static Integer[][] checkerBoard = new Integer[8][8];
  // 棋盤副本
  public static Integer[][] checkerBoardCopy = new Integer[8][8];

  // 計數器 用於計數有多少種方法
  public static Integer jishu = 1;

  // 定義橫豎斜方向上是否有棋子
  public static boolean flag1 = true;
  public static boolean flag2 = true;
  public static boolean flag3 = true;
  public static boolean flag4 = true;

  // 初始化一個棋盤 8x8
  public static void init() {
    for (int i = 0; i < 8; i++) {
      for (int j = 0; j < 8; j++) {
        System.out.print(0 + "  ");
        checkerBoard[i][j] = 0;
      }
      System.out.println();
    }
    checkerBoardCopy = checkerBoard;
  }

  // 遞迴測試方法
  public static void startTest(int row) {
    for (int col = 0; col < 8; col++) {
      if (checkCheet(row,col,checkerBoardCopy) == 1) {
        if (row < 7) {
          startTest(++row);
          --row;
        }
      }
      // 該行重新賦值為0  進行下一次判斷
      checkerBoardCopy[row][col] = 0;
    }
  }

  // 檢查是否危險
  // row行
  // col列
  public static int checkCheet(int row,int col,Integer[][] checkerBoardCopy) {
    flag1 = true;
    flag2 = true;
    flag3 = true;
    flag4 = true;
    // 行方向上是否滿足條件
    for (int i = 0; i < 8; i++) {
      if (checkerBoardCopy[row][i] == 1) {
        flag1 = false;
        break;
      }
    }
    // 列方向上是否滿足條件
    for (int j = 0; j < 8; j++) {
      if (checkerBoardCopy[j][col] == 1) {
        flag2 = false;
        break;
      }
    }
    // 右下方向
    for (int i = row,j = col; i < 8 & j < 8; i++,j++) {
      if (checkerBoardCopy[i][j] == 1) {
        flag3 = false;
        break;
      }
    }
    // 左上方向
    for (int i = row,j = col; i >= 0 & j >= 0; i--,j--) {
      if (checkerBoardCopy[i][j] == 1) {
        flag3 = false;
        break;
      }
    }
    // 左下方向
    for (int i = row,j = col; i < 8 & j >= 0; i++,j--) {
      if (checkerBoardCopy[i][j] == 1) {
        flag4 = false;
        break;
      }
    }
    // 右上方向
    for (int i = row,j = col; i >= 0 & j < 8; i--,j++) {
      if (checkerBoardCopy[i][j] == 1) {
        flag4 = false;
        break;
      }
    }
    if (flag1 & flag2 & flag3 & flag4) {
      // 若為真 增此點的值賦為1
      checkerBoardCopy[row][col] = 1;
      // 如果已經判斷到最後一行 並且最後一行也符合情況 列印整個棋盤
      if (row == 7) {
        printCheets(checkerBoardCopy);
      }
      return 1;
    }
    return 0;
  }

  // 列印棋盤方法
  public static void printCheets(Integer[][] checkerBoardCopy) {
    for (int i = 0; i < 8; i++) {
      for (int j = 0; j < 8; j++) {
        System.out.print(checkerBoardCopy[i][j] + "  ");
      }
      System.out.println();
    }
    System.out.println("=================" + jishu++);
  }

  public static void main(String[] args) {
    init();
    startTest(0);
  }
}

copy後可直接執行。 記一下這次的程式碼。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。