Pagini recente » Cod sursa (job #1937198) | Cod sursa (job #1859415) | Cod sursa (job #490686) | Cod sursa (job #2170772) | Cod sursa (job #2431487)
#include <fstream>
using namespace std;
ifstream fin("damesah.in");
ofstream fout("damesah.out");
int n, sol[15],prima, nr;
int Valid(int k)
{
int i, x, y;
for(i = 1; i < k; i++)
if(sol[i] == sol[k]) return 0;
x=k-1;
y=sol[k]-1;
while(x>=1 && y >=1)
{
if (sol[x]==y) return 0;
else {x--;y--;}
}
x = k - 1;
y = sol[k] + 1;
while(x >= 1 && y <= n)
{
if(sol[x] == y) return 0;
else
{
x--;
y++;
}
}
return 1;
}
void Afisare()
{
for(int i = 1; i <= n; i++)
fout << sol[i] << " ";
fout << "\n";
}
void Back(int k)
{
int i;
for(i = 1; i <= n; i++)
{
sol[k]=i;
if(Valid(k)==1)
{
if(k == n)
{
nr++;
if(prima==0)
{
Afisare();
prima = 1;
}
}
Back(k+1);
}
}
}
int main()
{
fin>>n;
Back(1);
fout << nr;
return 0;
}