/* * Solve the n-Pieces problem of trying to place n identical pieces on * an n x n chessboard. * * Knights Bishops Rooks Queens Amazons * 2 6 4 2 0 0 * 3 36 26 6 0 0 * 4 412 260 24 2 0 * 5 9386 3368 120 10 0 * 6 257318 53744 720 4 0 * 7 8891854 1022320 5040 40 0 * 8 379978716 22522960 40320 92 0 * 9 19206532478 565532992 362880 352 0 * 10 1120204619108 15915225216 3628800 724 4 */ # include # include "Queens.h" int n; // size of the chessboard Piece **pieces; // array of pointers to chess pieces long long count; // number of solutions found so far /* * Place the current piece on the chessboard starting at the specified * starting row and column. */ void place (int start_row, int start_col, int current) { int row, col, i; for (row = start_row; row < n; row ++) { for (col = start_col; col < n; col ++) { pieces [current] -> place (row, col); /* * Check if we are placed at the same location as or if we * menace any previously placed piece. */ for (i = 0; i < current; i ++) { if (pieces [i] -> row ( ) == row && pieces [i] -> column ( ) == col) break; if (pieces [current] -> menaces (pieces [i])) break; } /* * If we successfully placed ourselves, then try to place * the next piece if one exists. If no next piece exists * then we have found a solution. */ if (i == current) if (current + 1 != n) place (row, col + 1, current + 1); else count ++; } start_col = 0; } } int main ( ) { cin >> n; pieces = new Piece * [n]; for (int i = 0; i < n; i ++) pieces [i] = new Queen; place (0, 0, 0); cout << count << endl; }