Advertisement
xerpi

Problem P16415_ca: Reines (1)

Dec 19th, 2014
605
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6.  
  7. bool legal(const vector<int> &sp, int q, int n, int y,
  8.     const vector<bool> &fila,
  9.     const vector<bool> &diag_pos,
  10.     const vector<bool> &diag_neg)
  11. {
  12.     return not (fila[y] or diag_pos[q+y] or diag_neg[(n-y)+q-1]);
  13. }
  14.  
  15. void reines(vector<int> &sp, int q, int n, int &cnt,
  16.     vector<bool> &fila,
  17.     vector<bool> &diag_pos,
  18.     vector<bool> &diag_neg)
  19. {
  20.     if (q == n) cnt++;
  21.     else {
  22.         for (int y = 0; y < n; y++) {
  23.             if (legal(sp, q, n, y, fila, diag_pos, diag_neg)) {
  24.                 sp[q] = y;
  25.                 fila[y] = true;
  26.                 diag_pos[q+y] = true;
  27.                 diag_neg[(n-y)+q-1] = true;
  28.                 reines(sp, q+1, n, cnt, fila, diag_pos, diag_neg);
  29.                 diag_neg[(n-y)+q-1] = false;
  30.                 diag_pos[q+y] = false;
  31.                 fila[y] = false;
  32.                 sp[q] = -1;
  33.             }
  34.         }
  35.     }
  36. }
  37.  
  38. int main()
  39. {
  40.     int n, cnt = 0;
  41.     cin >> n;
  42.     vector<bool> fila(n, false),
  43.         diag_neg(2*n-1, false),
  44.         diag_pos(2*n-1, false);
  45.     vector<int> sp(n, -1);
  46.     reines(sp, 0, n, cnt, fila, diag_pos, diag_neg);
  47.     cout << cnt << endl;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement