Pagini recente » Statistici Andrunache Dragos Stefan (Andrunace_Stefan) | Statistici Muresan Cristian (christu) | Monitorul de evaluare | Monitorul de evaluare | Cod sursa (job #1732868)
#include <fstream>
using namespace std;
ifstream in("damesah.in");
ofstream out("damesah.out");
int st[10], n, sol = 0, Map[10][10];
void Afisare()
{
int i;
for (i = 1; i <= n; i++)
out << st[i] << " ";
out << endl;
sol++;
}
bool verificareLinie(int linie, int coloana) {
for (int i = 1; i <= n; i++) {
if (i!=coloana && Map[linie][i] != 0) return false;
}
return true;
}
bool verificareColoana(int linie, int coloana) {
for (int i = 1; i <= n; i++) {
if (i!=linie && Map[i][coloana] != 0) return false;
}
return true;
}
bool verificareDiagonale(int pozx, int pozy) {
int p=pozx-1, pp=pozy+1 ;
while(p>0){
if (Map[p][p] != 0) return false;
p--;
}
while (pp <= n) {
if (Map[pp][pp] != 0) return false;
pp++;
}
p = pozx - 1; pp = pozy + 1;
while (p > 0 && pp <= n) {
if (Map[p][pp] != 0) return false;
p--; pp++;
}
p = pozx + 1; pp = pozy - 1;
while (p <= n && pp > 0) {
if (Map[p][pp] != 0) return false;
p++; pp--;
}
return true;
}
int Valid(int k)
{
for (int i = 1; i <= k - 1; i++) {
if (st[k] == st[i]) return 0;
}
return 1;
}
void restaurareMatrice() {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
Map[i][j] = 0;
}
}
}
void Write() {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
out << Map[i][j] << " ";
}
out << "\n";
}
}
int analizare() {
restaurareMatrice();
for (int i = 1; i <= n; i++) {
Map[i][st[i]] = 1;
}
for (int i = 1; i <= n; i++) {
if (verificareLinie(i, st[i]) == 0 || verificareColoana(i, st[i]) == 0 || verificareDiagonale(i, st[i]) == 0) return 0;
}
return 1;
}
void Back(int k)
{
int i;
for (i = 1; i <= n; i++)
{
st[k] = i;
if (Valid(k))
if (k == n) {
if (analizare()) Afisare();
}
else Back(k + 1);
}
}
int main()
{
in >> n;
Back(1);
out << sol;
return 0;
}