Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- class Task {
- public:
- vector<int> aux;
- void solve() {
- read_input();
- print_output(get_result());
- }
- private:
- int n;
- void read_input() {
- ifstream fin("in");
- fin >> n;
- fin.close();
- }
- bool check(vector<int> &v) {
- int last = v.size() - 1;
- for (int i = 1; i < last; ++i) {
- if (v[i] == v[last] || (abs(v[last] - v[i]) == last - i))
- return false;
- }
- return true;
- }
- void back(int step, vector<int> &sol) {
- for (int i = 1; i <= n; ++i) {
- aux.push_back(i);
- if (check(aux)) {
- if (step == n) {
- sol = aux;
- return;
- }
- back(step + 1, sol);
- }
- aux.pop_back();
- }
- }
- vector<int> get_result() {
- vector<int> sol(n + 1, 0);
- back(1, sol);
- /*
- TODO: Gasiti o solutie pentru problema damelor pe o tabla de dimensiune
- n x n.
- Pentru a plasa o dama pe randul i, coloana j:
- sol[i] = j.
- Randurile si coloanele se indexeaza de la 1 la n.
- De exemplu, configuratia (n = 5)
- X----
- --X--
- ----X
- -X---
- ---X-
- se va reprezenta prin sol[1..5] = {1, 3, 5, 2, 4}.
- */
- 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();
- }
- };
- int main() {
- Task task;
- task.solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement