Cod sursa(job #3250851)

Utilizator KRISTY06Mateiu Ianis Cristian Vasile KRISTY06 Data 23 octombrie 2024 20:58:39
Problema Problema Damelor Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.8 kb
#include <iostream>
#include <fstream>
#include <cstring>
#include <algorithm>
#include <deque>
#include <iomanip>
#include <queue>
#include <cmath>
#include <map>
#include <vector>
#include <bitset>
#include <unordered_map>
#include <set>
using namespace std;

ifstream fin("damesah.in");
ofstream fout("damesah.out");

int n, isSelected[14], ansCounter;

vector<int> ans(14, 0), minAns(14, 14);

bool isValid() {
    int mt[14][14] = {0};
    for (int i = 1; i <= n; ++i) {
        mt[i][ans[i]] = 1;
    }
    for (int i = 1; i <= n; ++i) {
        for (int line = i - 1, col = ans[i] - 1; line >= 1 && col >= 1; --line, --col) {
            if (mt[line][col] == 1) {
                return 0;
            }
        }
        for (int line = i + 1, col = ans[i] + 1; line <= n && col <= n; ++line, ++col) {
            if (mt[line][col] == 1) {
                return 0;
            }
        }
        for (int line = i + 1, col = ans[i] - 1; line <= n && col >= 1; ++line, --col) {
            if (mt[line][col] == 1) {
                return 0;
            }
        }
        for (int line = i - 1, col = ans[i] + 1; line >= 1 && col <= n; --line, ++col) {
            if (mt[line][col] == 1) {
                return 0;
            }
        }
    }
    return 1;
}

void findAns(int ansLen) {
    if (ansLen == n + 1) {
        if (isValid() == 1) {
            ++ansCounter;
            minAns = min(minAns, ans);
        }
        return;
    }
    for (int col = 1; col <= n; ++col) {
        if (isSelected[col] == 0) {
            isSelected[col] = 1;
            ans[ansLen] = col;
            findAns(ansLen + 1);
            isSelected[col] = 0;
        }
    }
}

int main() {
    fin >> n;
    findAns(1);
    for (int i = 1; i <= n; ++i) {
        fout << minAns[i] << ' ';
    }
    fout << '\n' << ansCounter;
    return 0;
}