Pagini recente » Cod sursa (job #338666) | Cod sursa (job #2759094) | Cod sursa (job #1958591) | Cod sursa (job #237643) | Cod sursa (job #2682149)
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("damesah.in");
ofstream out("damesah.out");
int n, ans, offset = 15;
int v[15];
bool fr[15], diagPr[35], diagSec[15];
void print()
{
for(int i = 1; i <= n; i++)
out << v[i] << ' ';
out << '\n';
}
bool check(int index)
{
//cout << index << '\n';
if(fr[v[index]] == true || diagPr[index - v[index] + offset] == true || diagSec[index + v[index]] == true)
return false;
//cout << "true" << '\n';
fr[v[index]] = true;
diagPr[index - v[index] + offset] = true;
diagSec[index + v[index]] = true;
return true;
}
void sah(int index)
{
for(v[index] = 1; v[index] <= n; v[index]++)
{
/*cout << "INDEX: " << index << '\n';
cout << "VALUE: " << v[index] << '\n';
cout << fr[v[index]] << ' ' << diagPr[index - v[index] + offset] << ' ' << diagSec[index + v[index]] << "\n\n";*/
if(index == n + 1)
{
if(ans == 0)
print();
ans++;
return;
}
else if(check(index))
{
sah(index + 1);
fr[v[index]] = false;
diagPr[index - v[index] + offset] = false;
diagSec[index + v[index]] = false;
}
}
}
int main()
{
in >> n;
sah(1);
out << ans << '\n';
return 0;
}