Pagini recente » Cod sursa (job #1659394) | Cod sursa (job #2592339) | Cod sursa (job #651450) | Cod sursa (job #2266144) | Cod sursa (job #2502342)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("damesah.in");
ofstream fout("damesah.out");
int n,afisat;
bool a[15][15];
void afisare() {
int i, j;
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
if (a[i][j] == 1)
cout<<j<<' ';
}
int ok(int x, int y) {
int i, j;
for (j = 1; j <= n; j++)
if (a[x][j] == 1)
return 0;
for (i = 1; i <= n; i++)
if (a[i][y] == 1)
return 0;
i = x + 1;
j = y + 1;
while (i <= n && j <= n) {
if (a[i][j] == 1)
return 0;
i++;
j++;
}
i = x - 1;
j = y - 1;
while (i >= 1 && j >= 1) {
if (a[i][j] == 1)
return 0;
i--;
j--;
}
i = x + 1;
j = y - 1;
while (i <= n && j >= 1) {
if (a[i][j] == 1)
return 0;
i++;
j--;
}
i = x - 1;
j = y + 1;
while (i >= 1 && j <= n) {
if (a[i][j] == 1)
return 0;
i--;
j++;
}
return 1;
}
void back(int x) {
int i, j;
if (x == n + 1) {
if(afisat == 0) {
afisat++;
afisare();
}
else
afisat++;
} else {
for (i = x; i <= n; i++)
for (j = 1; j <= n; j++)
if (ok(i, j)) {
a[i][j] = 1;
back(x + 1);
a[i][j] = 0;
}
}
}
int main() {
fin >> n;
back(1);
fout<<'\n';
fout<<afisat;
return 0;
}