/* *************************************** Title: 藍橋杯練習題—— 2n皇後問題 *************************************** Date:2014/03/17 *************************************** author:劉旭 *************************************** */ #includeusing namespace std; #define N 100 int wq[N]; //whitequeen,黑皇後位置 int bq[N]; //blackqueen,白皇後位置 int cb[N][N]; //chessboard,棋盤 int num; //皇後數目 int count = 0; //不同放置情況計數 int bqueen(int pos) //白色皇後放置 { int i; for(i = 0; i < pos -1; i++) { int judge = bq[i] - bq[pos-1]; if(0 == judge || judge == pos - 1 - i || -judge == pos - 1 - i) return 0; } if(pos == num) { count++; return 0; } for(int i = 0; i < num; i++) { if(i != wq[pos] && cb[pos][i]) { bq[pos] = i; bqueen(pos+1); } } } int wqueen(int pos) //黑色皇後放置 { int i; for(i = 0; i < pos -1; i++) { int judge = wq[i] - wq[pos-1]; if(0 == judge || judge == pos - 1 - i || -judge == pos - 1 - i) return 0; } if(pos == num) { bqueen(0); return 0; } for(int i = 0; i < num; i++) { if(cb[pos][i]) { wq[pos] = i; wqueen(pos+1); } } } int main() { cin>>num; for(int i =0; i < num; i++) for(int j = 0; j < num; j++) cin>>cb[i][j]; wqueen(0); cout<