N queen question, n queen
Description
Place n queens that are not under attack on the chessboard of n × n grids. According to the rules of chess, the queen can attack pawns in the same row, column, or diagonal line. The problem after n is equivalent to placing n on the n × n board. Any two queens may wish to be on the same row, column, or diagonal line.
Input/Output Format
Input description:
Given the size of the Board n (n ≤ 13)
Output description:
The output integer indicates the number of placement methods.
Input and Output sample
Input example #1:
8
Output sample #1:
92
Ideas
Search.
Code # include <stdio. h> int a [200] = {0}, B [200] = {0}, c [200] = {0}, d [200], p = 0; int search (int n, int s) {int I; for (I = 1; I <= n; I ++) {if (a [I] = 0 & B [I + s] = 0 & c [s-I + n-1] = 0) {a [I] = 1; B [I + s] = 1; c [s-I + n-1] = 1; d [s] = I; if (s = n) p ++; else search (n, s + 1); a [I] = 0; B [I + s] = 0; c [s-I + n-1] = 0 ;}} int main () {int n; scanf ("% d", & n); search (n, 1 ); if (p = 0) printf ("no solute! "); Else printf (" % d ", p); return 0;} View Code