实现代码:
#include <iostream>
#include <cstdio>
#include <stack>
#include <cmath>
using namespace std;
bool verify(int *Q, int index);
void outPut(int *Q, int len);
int main()
{
int n;
while(scanf("%d", &n) != EOF)
{
int cnt = 0;
stack<int> st;
int *queen = new int[n];
int row = 0;
queen[0] = -1;
st.push(0);
while(!st.empty())
{
row = st.top();
queen[row]++;
if(queen[row] >= n)
st.pop();
else
{
if(verify(queen, row))
{
if(row == n - 1)
{
outPut(queen, n);
cnt++;
}
else
{
queen[row + 1] = -1;
st.push(row + 1);
}
}
}
}
printf("The kind is:%d\n\n\n", cnt);
delete []queen;
}
return 0;
}
bool verify(int *Q, int index)
{
for(int i = 0; i < index; i++)
{
if(Q[i] == Q[index] || abs(index - i) == abs(Q[index] - Q[i]))
return false;
}
return true;
}
void outPut(int *Q, int len)
{
for(int i = 0; i < len; i++)
printf("(%d, %d) ", i, Q[i]);
printf("\n");
}