Pagini recente » Sandbox (cutiuţa cu năsip) | Cod sursa (job #2430649) | Cod sursa (job #817673) | Cod sursa (job #557803) | Cod sursa (job #2429252)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("damesah.in");
ofstream fout("damesah.out");
const int CMAX = 15;
int n , v[CMAX] , rasp = 0 , mat[CMAX][CMAX];
bool ok = 0;
void citire()
{
fin >> n;
}
bool verificare(int i , int j)
{
int k = 1;
while(i-k>=1)
{
if(mat[i-k][j-k]==1)
return false;
if(mat[i-k][j+k]==1)
return false;
if(mat[i-k][j]==1)
return false;
k++;
}
return true;
}
void afisare()
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
cout << mat[i][j] << " ";
cout << '\n';
}
cout << '\n';
}
void bkt(int k)
{
for(int i=1;i<=n;i++)
{
v[k] = i;
mat[k][i] = 1;
if(verificare(k,i)==true)
{
if(k==n)
{
if(ok==0){
for(int j=1;j<=n;j++)
fout << v[j] << " ";
ok = 1;
}
rasp++;
}
else bkt(k+1);
}
mat[k][i] = 0;
}
}
int main()
{
citire();
bkt(1);
fout << '\n' << rasp;
return 0;
}