Pagini recente » Sport | Cod sursa (job #3220564) | Cod sursa (job #2472414) | Cod sursa (job #2718864) | Cod sursa (job #2830914)
#include<fstream>
using namespace std;
ifstream f("copii.in");
ofstream g("copii.out");
int n;
char ch, a[20][20];
int nr_sol, sol[25];
bool v[25][25];
void solutii(int m) {
int i, j;
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= m; j++) {
v[i][j] = false;
}
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (a[i][j] == '1')
v[sol[i]][sol[j]] = true;
for (int i = 1; i <= m; i++)
for (int j = 1; j <= m; j++)
if (i != j && v[i][j] == false)
return;
nr_sol++;
}
void backtr(int p, int q) {
if (p == n + 1) {
bool is_solution = true;
for (int i = 1; i <= q; i++) {
for (int j = 1; j <= q; j++) {
v[i][j] = false;
}
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (a[i][j] == '1')
v[sol[i]][sol[j]] = true;
for (int i = 1; i <= q; i++)
for (int j = 1; j <= q; j++)
if (i != j && v[i][j] == false) {
is_solution = false;
break;
}
if (is_solution == true)
nr_sol++;
return;
}
for (int i = 1; i <= q; i++) {
sol[p] = i;
backtr(p + 1, q);
}
sol[p] = q + 1;
backtr(p + 1, q + 1);
}
int main() {
f >> n;
f.get();
for (int i = 1; i <= n; i++)
f.getline(1 + a[i], 20);
backtr(1, 0);
g << nr_sol - 1;
return 0;
}