Pagini recente » Borderou de evaluare (job #3353071) | Monitorul de evaluare | Monitorul de evaluare | Monitorul de evaluare | Cod sursa (job #3355098)
// Ionascu George-Razvan, 324CA
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
vector <vector <int>> a;
ifstream fin("damesah.in");
ofstream fout("damesah.out");
bool col_empty(int n, int col) {
for (int i = 0; i < n; i++) {
if (a[i][col] == 1) {
return false;
}
}
return true;
}
bool diag_empty(int n, int curr_x, int curr_y) {
int tmpx, tmpy;
//stanga sus
tmpx = curr_x - 1;
tmpy = curr_y - 1;
while(tmpx >= 0 && tmpy >= 0) {
if (a[tmpx][tmpy] == 1) {
return false;
}
tmpx--;
tmpy--;
}
//dreapta sus
tmpx = curr_x - 1;
tmpy = curr_y + 1;
while(tmpx >= 0 && tmpy < n) {
if (a[tmpx][tmpy] == 1) {
return false;
}
tmpx--;
tmpy++;
}
return true;
}
void bkt(int n, int start, bool &printed, int &cnt) {
if (start == n) {
if (printed == false) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (a[i][j] == 1) {
fout << j + 1 << " ";
}
}
}
fout << "\n";
printed = true;
}
cnt++;
return;
}
for (int i = 0; i < n; i++) {
if (col_empty(start, i) == true && diag_empty(n, start, i) == true) {
a[start][i] = 1;
bkt(n, start + 1, printed, cnt);
a[start][i] = 0;
}
}
}
int main() {
int n, cnt = 0;
bool printed = false;
fin >> n;
a.resize(n, vector<int> (n, 0));
bkt(n, 0, printed, cnt);
fout << cnt << "\n";
fin.close();
fout.close();
return 0;
}