Problem description placed n Queens on the N*n's checkered chessboard, making them not attack each other (that is, any 2 queens are not allowed to be in the same row, in the same column, or in a diagonal line with a 45-angle checkerboard border.)
Your task is to find out how many legal placement methods are available for a given n.
Input has several lines, one positive integer n≤10 per line, indicating the number of boards and queens, or, if n=0, the end.
Output has several rows, one positive integer per line, representing the number of different placements of the queen corresponding to the input row.
Sample Input1850//Still not quite understand. Recent mood ups and downs, today at noon also figured out, even if there are more unhappy, life or want to continue to work, do not tube, efforts, Rubik's Cube!! Acm!!
#include <stdio.h>#include<string.h>#include<iostream>using namespacestd;intn,tmp;intmap[ One];voidDfsintk) { intI,j,flag; if(k==n+1) {tmp++; return ; } Else { for(i=1; i<=n;i++) {Map[k]=i; Flag=1; for(j=1; j<k;j++) { if(map[j]==i| | i-k==map[j]-j| | i+k==map[j]+j) {Flag=0; Break; } } if(flag) DFS (k+1); } }}intMain () {inti,m; intans[ One]; for(n=1; n<=Ten; n++) {tmp=0; DFS (1); Ans[n]=tmp; } while(cin>>m&&m) {cout<<ans[m]<<Endl; } return 0;}
Hdu 2553 N Queen question