Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. #include <fstream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. class Task {
  7. public:
  8. vector<int> aux;
  9. void solve() {
  10. read_input();
  11. print_output(get_result());
  12. }
  13.  
  14. private:
  15. int n;
  16.  
  17. void read_input() {
  18. ifstream fin("in");
  19. fin >> n;
  20. fin.close();
  21. }
  22.  
  23. bool check(vector<int> &v) {
  24. int last = v.size() - 1;
  25. for (int i = 1; i < last; ++i) {
  26. if (v[i] == v[last] || (abs(v[last] - v[i]) == last - i))
  27. return false;
  28. }
  29. return true;
  30. }
  31.  
  32. void back(int step, vector<int> &sol) {
  33. for (int i = 1; i <= n; ++i) {
  34. aux.push_back(i);
  35. if (check(aux)) {
  36. if (step == n) {
  37. sol = aux;
  38. return;
  39. }
  40. back(step + 1, sol);
  41. }
  42. aux.pop_back();
  43. }
  44. }
  45.  
  46. vector<int> get_result() {
  47. vector<int> sol(n + 1, 0);
  48. back(1, sol);
  49.  
  50. /*
  51. TODO: Gasiti o solutie pentru problema damelor pe o tabla de dimensiune
  52. n x n.
  53.  
  54. Pentru a plasa o dama pe randul i, coloana j:
  55. sol[i] = j.
  56. Randurile si coloanele se indexeaza de la 1 la n.
  57.  
  58. De exemplu, configuratia (n = 5)
  59. X----
  60. --X--
  61. ----X
  62. -X---
  63. ---X-
  64. se va reprezenta prin sol[1..5] = {1, 3, 5, 2, 4}.
  65. */
  66.  
  67. return sol;
  68. }
  69.  
  70. void print_output(vector<int> result) {
  71. ofstream fout("out");
  72. for (int i = 1; i <= n; i++) {
  73. fout << result[i] << (i != n ? ' ' : '\n');
  74. }
  75. fout.close();
  76. }
  77. };
  78.  
  79. int main() {
  80. Task task;
  81. task.solve();
  82. return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement