Problem : 2553 ( N皇后问题 ) Judge Status : Accepted
RunId : 2619754 Language : G++ Author : huwenbiao
Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta
RunId : 2619754 Language : G++ Author : huwenbiao
Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta
/***************************************************************\
*Author:Hu Wenbiao
*Created Time: Sat 17 Jul 2010 10:35:51 AM CST
*File Name: a.cpp
*Description:回溯
\***************************************************************/
//*========================*Head File*========================*\\
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/*----------------------*Global Variable*----------------------*/
int X[11],N,sum;
//*=======================*Main Program*=======================*//
using namespace std;
bool place(int k){
for(int j=1;j<k;++j){
if(X[j]==X[k]||abs(k-j)==abs(X[k]-X[j]))
return false;
}
return true;
}
void backtrack(int k){
if(k>N)
sum++;
else{
for(int i=1;i<=N;++i){
X[k]=i;
if(place(k)){
backtrack(k+1);
}
}
}
}
int main(){
//freopen("input","r",stdin);
int ans[11];
for(N=1;N<=10;++N){
sum=0;
backtrack(1);
ans[N]=sum;
}
while(scanf("%d",&N)!=-1&&N){
printf("%d\n",ans[N]);
}
}