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:
- void solve() {
- read_input();
- print_output(get_result());
- }
- private:
- int n;
- vector<int> mysol;
- void read_input() {
- ifstream fin("in");
- fin >> n;
- fin.close();
- }
- int myabs(int x) {
- if(x < 0) {
- return -x;
- }
- return x;
- }
- int check(std::vector<int> sol, int k) {
- for(int i = 1; i < k; i++) {
- if(sol[k] == sol[i]) {
- return 0;
- }
- if((k - i) == myabs(sol[k] - sol[i])) {
- return 0;
- }
- }
- return 1;
- }
- void bkt(std::vector<int> &sol, int k, int &ok) {
- if(k == n + 1) {
- if(ok == 0) {
- mysol = sol;
- ok = 1;
- }
- } else {
- for(int i = 1; i <= n; i++) {
- sol[k] = i;
- if(check(sol, k) == 1) {
- bkt(sol, k + 1, ok);
- }
- }
- }
- }
- vector<int> get_result() {
- vector<int> sol(n + 1, 0);
- mysol.resize(n + 1);
- int ok = 0;
- bkt(sol, 1, ok);
- /*
- 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 mysol;
- }
- 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