Cod sursa(job #2590458)

Utilizator vvvaliiiValeriu vvvaliii Data 27 martie 2020 23:36:05
Problema Problema Damelor Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.84 kb
#include <bits/stdc++.h>
using namespace std;

class Task {
 public:
    void solve() {
        read_input();
        print_output(get_result());
    }

 private:
    int n;

    void read_input() {
        ifstream fin("in");
        fin >> n;
        fin.close();
    }

    bool check(int pos, vector<int> v){
        if(v[v.size() - 2] == 0)
            return false;
        for(int i = 0 ; i < v.size() - 1; i++){
            if(v[i] == 0)
                return false;
            for(int j = i + 1; j < v.size() - 1; j++){
                if(v[i] + (j - i)== v[j] || v[i] - (j - i) == v[j])
                    return false;
            }
        }
        return true;
    }

    bool ver(int val, vector<int> v){
        for(int i = 0; i < v.size() - 1; i++){
            if(v[i] == val)
                return true;
        }
        return false;
    }
    void back(int pos, vector<int> &solution) {

        if (check(pos, solution)) {
            solution.insert(solution.begin(), 0);
            print_output(solution);
            exit(0);
        }

        for(int i = 1; i <= n; ++i) {
            if(ver(i, solution))
                continue;
            solution[pos] = i;
            back(pos + 1, solution);
        }
        solution[pos] = 0;
    }

    vector<int> get_result() {
        vector<int> sol(n + 1 , 0);
        
        back(0, sol);
        return sol;
    }

    void print_output(vector<int> result) {
        ofstream fout("out");
        for (int i = 1; i <= n; i++) {
            fout << result[i] << (i != n ? ' ' : '\n');
        }
        fout.close();
    }
};

// Please always keep this simple main function!
int main() {
    // Allocate a Task object on heap in order to be able to
    // declare huge static-allocated data structures inside the class.
    Task *task = new Task();
    task->solve();
    delete task;
    return 0;
}