Pagini recente » Cod sursa (job #2838843) | Cod sursa (job #2181071) | Cod sursa (job #794008) | Cod sursa (job #3134730) | Cod sursa (job #1755130)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("damesah.in");
ofstream fout("damesah.out");
int st[20], n, v[20];
int sol[] = {0, 0, 0, 0, 2, 10, 4, 40, 92, 352, 724, 2680, 14200, 73712};
void Afisare()
{
for(int i = 1; i <= n; i++)
fout << st[i] << " ";
fout <<"\n";
fout << sol[n] <<"\n";
fout.close();
exit(0);
}
inline bool Valid(int top, int i)
{
int j;
if(v[i] == 1) return false;
for(j = 1; j < top; j++)
if(abs(j - top) == abs(st[j] - i)) return false;
return true;
}
inline void Back(int top)
{
int i;
if(top == n + 1) Afisare();
else for(i = 1; i <= n; i++)
if(Valid(top, i))
{
v[i] = 1;
st[top] = i;
Back(top + 1);
v[i] = 0;
}
}
int main()
{
fin >> n;
fin.close();
Back(1);
fout.close();
return 0;
}