Pagini recente » Cod sursa (job #821532) | Istoria paginii runda/dedicatie_speciala6/clasament | Cod sursa (job #252033) | Cod sursa (job #2422401) | Cod sursa (job #1826349)
#include <fstream>
#include <cmath>
using namespace std;
ifstream fin("damesah.in");
ofstream fout("damesah.out");
short n, ans;
bool xdame[14];
short ydame[14];
void scrie()
{
for(short i=1; i<=n; i++)
fout<<ydame[i]<<' ';
}
bool valid(short k, short i)
{
bool ret=1;
for(short w=1; w<k; w++)
if((ydame[w]==i)||(k-w==abs(i-ydame[w])))
return 0;
return ret;
}
void bcktrk(short k)
{
if(k!=0)
{
for(short w=1; w<=n; w++)
{
if(valid(k, w))
{
xdame[k]=1;
ydame[k]=w;
if(k==n)
{
if(!ans)
scrie();
ans++;
}
else
bcktrk(k+1);
}
}
}
}
int main()
{
fin>>n;
bcktrk(1);
fout<<endl<<ans;
}