日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

皇后問(wèn)題

 shaobin0604@163.com 2006-08-29
  1. public class Queen {
  2.     private char[][] matrix;
  3.     private int size;
  4.     private int[] position;
  5.     private int result;
  6.  
  7.     public int getResult() {
  8.         return result;
  9.     }
  10.  
  11.     public Queen(int size) {
  12.         this.size = size;
  13.         matrix = new char[size][size];
  14.         for (int i = 0i < sizei++) {
  15.             for (int j = 0j < sizej++) {
  16.                 matrix[i][j] = x;
  17.             }
  18.         }
  19.         position = new int[size];
  20.     }
  21.  
  22.     private void printMatrix() {
  23.         System.out.printf("---- sulution %d ----\n"result);
  24.         for (int i = 0i < sizei++) {
  25.             System.out.print("{");
  26.             for (int j = 0j < size - 1j++)
  27.                 System.out.print(matrix[i][j] + " ");
  28.             System.out.print(matrix[i][size - 1]);
  29.             System.out.println("}");
  30.         }
  31.     }
  32.  
  33.     private boolean isValid(int rowint col) {
  34.         for (int i = 0i < rowi++) {
  35.             if (position[i] == position[row]) {
  36.                 return false;
  37.             }
  38.             if (Math.abs(position[i] - position[row]) == row - i) {
  39.                 return false;
  40.             }
  41.         }
  42.         return true;
  43.     }
  44.  
  45.     public void trial(int row) {
  46.         for (int i = 0i < sizei++) {
  47.             matrix[row][i] = .;
  48.             position[row] = i;
  49.             if (isValid(rowposition[row])) {
  50.                 if (row < size - 1) {
  51.                      trial(row + 1);
  52.                 } else {
  53.                     /* success */
  54.                     result++;
  55.                     printMatrix();
  56.                 }
  57.             } 
  58.             matrix[row][i] = x;
  59.         }
  60.     }
  61.  
  62.     public static void main(String[] args) {
  63.         Queen test = new Queen(4);
  64.         test.trial(0);
  65.         System.out.printf("result is: %d"test.getResult());
  66.     }
  67. }

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多