Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.57 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <utility>
  4. #include <cmath>
  5. using namespace std;
  6.  
  7. bool isWay (vector < pair < int, int > > &points, int n) {
  8.     bool is_way = true;
  9.     for (int f = 0; f < n - 1; f++) {
  10.         int x0 = points[f].first;
  11.         int y0 = points[f].second;
  12.         for (int s = f + 1; s < n; s++) {
  13.             int x1 = points[s].first;
  14.             int y1 = points[s].second;
  15.             if (x0 == x1 || y0 == y1 || abs(x0 - x1) == abs(y0 - y1)) {
  16.                 is_way = false;
  17.                 break;
  18.             }
  19.         }
  20.         if (!is_way) {
  21.             break;
  22.         }
  23.     }
  24.     return is_way;
  25. }
  26.  
  27. int getNumberOfCombinationsByPos (int &n, int x, int y, vector < pair < int, int > > &points) {
  28.     int combinations = 0;
  29.     points[x] = {x, y};
  30.  
  31.     if (!isWay(points, x + 1) || x == n - 1) {
  32.         if (isWay(points, x + 1)) {
  33.             combinations = combinations + 1;
  34.         }
  35.         return combinations;
  36.     }
  37.     for (int t = y - 2; t >= 0; t--) {
  38.         combinations += getNumberOfCombinationsByPos (n, x + 1, t, points);
  39.     }
  40.     for (int t = y + 2; t < n; t++) {
  41.         combinations += getNumberOfCombinationsByPos (n, x + 1, t, points);
  42.     }
  43.     return combinations;
  44. }
  45.  
  46. int main() {
  47.     int n, combinations = 0;
  48.     cin >> n;
  49.  
  50.     vector < pair < int, int > > points(n);
  51.  
  52.     for (int j = 0; j < n; j++) {
  53.         combinations += getNumberOfCombinationsByPos (n, 0, j,points);
  54.     }
  55.  
  56.     cout << combinations << endl;
  57.     // cout << (double)clock() / CLOCKS_PER_SEC << endl;
  58.     return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement