个人感觉代码还算精简,比较容易混淆的一点是,board[] 数组,数组下表指的是八皇后棋盘的行,数组下标对应的值指的是八皇后棋盘的列,至于abs()可以去百度,是一个求绝对值的函数
#include <iostream> using namespace std ; #define N 8 static int sum = 0 ; const int max = N ; void print (int board []) { for(int i = 0 ;i < max ;i++) { cout <<board[i]+1<<" " ; } cout<<endl; sum ++ ; } bool judge (int board[], int x ) { for (int i = 0 ; i< x ;i++) { if(board [i] == board[x] || abs(board[i] - board[x]) == (x-i)) { return false ; } } return true ; } void Queen (int board[] , int x) { if (x == max) { print (board) ; return ; } for (int i =0 ; i<max ;i++) { board [x] = i; if(judge(board, x)) { Queen(board,x+1) ; } } } int main () { int board[max] ; Queen(board , 0 ) ; cout << "total is :"<<sum ; return 0 ; }
原文地址:http://blog.csdn.net/hhooong/article/details/43348073