#include <iostream>
using namespace std;
int con[8] = {-1, -1, -1, 1, 1, -1, 1, 1};
const int N = 15;
int c[N][N];
int ans[N];
int n;
int row[N][N];
int col[N][N];
int bev[N][N];
int fun(int, int, int);
int res;
int main()
{
for (int i=0; i<N; i++)
{
ans[i] = -1;
}
while ((cin >> n) && n)
{
res = 0;
for (int i=0; i<n; i++)
{
for (int j=0; j<n; j++)
{
c[i][j] = 0;
row[i][j] = 0;
col[i][j] = 0;
bev[i][j] = 0;
}
}
if (ans[n] == -1)
{
ans[n] = fun(0, 0, n);
}
cout << ans[n] << endl;
}
return 0;
}
int fun(int i, int j, int chenum)
{
/* for (int u=0; u<n; u++)
{
for (int v=0; v<n; v++)
{
cout << c[u][v];
}
cout << " ";
for (int v=0; v<n; v++)
{
cout << row[u][v];
}
cout << " ";
for (int v=0; v<n; v++)
{
cout << col[u][v];
}
cout << " ";
for (int v=0; v<n; v++)
{
cout << bev[u][v];
}
cout << endl;
}
cout << endl;*/
if (!chenum)
{
res++;
//cout << "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrres: " << res << endl;
return res;
}
if ((i<n) && (j==n))
{
j = 0;
i++;
}
if (i == n)
{
return res;
}
c[i][j] = 1;
int minu, sum;
if ((!row[i][j]) && (!col[i][j]) && (!bev[i][j]))
{
minu = j-i;
sum = i+j;
for (int p=0; p<n; p++)
{
row[i][p]++;
col[p][j]++;
if (((sum-p)<n) && ((sum-p)>=0))
{
bev[p][sum-p]++;
}
if (((p+minu)<n) && ((p+minu)>=0))
{
bev[p][p+minu]++;
}
}
res = fun(i+1, 0, chenum-1);
for (int p=0; p<n; p++)
{
row[i][p]--;
col[p][j]--;
if (((sum-p)<n) && ((sum-p)>=0))
{
bev[p][sum-p]--;
}
if (((p+minu)<n) && ((p+minu)>=0))
{
bev[p][p+minu]--;
}
}
}
c[i][j] = 0;
if ((j == n-1) && (i+chenum > n-1))
{
return res;
}
res = fun(i, j+1, chenum);
return res;
}
//此乃降低空间和时间复杂度的优化代码
#include <iostream>
using namespace std;
const int N = 15;
int c[N][N];
int ans[N];
int n;
int row[N];
int col[N];
int bev[4*N];
int fun(int, int, int);
int res;
int main()
{
for (int i=0; i<N; i++)
{
ans[i] = -1;
}
while ((cin >> n) && n)
{
res = 0;
for (int i=0; i<n; i++)
{
row[i] = 0;
col[i] = 0;
for (int j=0; j<n; j++)
{
c[i][j] = 0;
}
}
for (int i=0; i<4*N; i++)
{
bev[i] = 0;
}
if (ans[n] == -1)
{
ans[n] = fun(0, 0, n);
}
cout << ans[n] << endl;
}
return 0;
}
int fun(int i, int j, int chenum)
{
if (!chenum)
{
res++;
return res;
}
if ((i<n) && (j==n))
{
j = 0;
i++;
}
if (i == n)
{
return res;
}
c[i][j] = 1;
int minu, sum;
if ((!row[i]) && (!col[j]) && (!bev[j-i+3*n]) && !(bev[i+j]))
{
row[i] = 1;
col[j] = 1;
bev[i+j] = 1;
bev[j-i+3*n] = 1;
res = fun(i+1, 0, chenum-1);
row[i] = 0;
col[j] = 0;
bev[i+j] = 0;
bev[j-i+3*n] = 0;
}
c[i][j] = 0;
if ((j == n-1) && (i+chenum > n-1))
{
return res;
}
res = fun(i, j+1, chenum);
return res;
}