dmkozyrev

Untitled

Mar 30th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7. const bool DEBUG = false;
  8.  
  9. vector<pair<int, int>> fixed_q, fixed_r;
  10.  
  11. bool is_diagonal(pair<int, int> a, pair<int, int> b) {
  12.     return std::abs(a.first - b.first) == std::abs(a.second - b.second);
  13. }
  14.  
  15. void go(int Row, int n, int q, int r, int& answer) {
  16.     if (q + r == 0) {
  17.         if (DEBUG) {
  18.             printf("R:");
  19.             for (auto & p : fixed_r) cout << " ("<<p.first<<","<<p.second<<")";
  20.             printf("\n");
  21.             printf("Q:");
  22.             for (auto & p : fixed_q) cout << " ("<<p.first<<","<<p.second<<")";
  23.             printf("\n");
  24.             printf("\n");
  25.         }
  26.            
  27.         ++answer;
  28.         return;
  29.     }
  30.     if (Row == n) return;
  31.     for (int row = Row; row <= n - q - r; ++row)
  32.     for (int col = 0; col < n; ++col) {
  33.         bool flag = true;
  34.         for (auto & p : fixed_q)
  35.             if (is_diagonal({row, col}, p) || col == p.second) {
  36.                 flag = false;
  37.                 break;
  38.             }
  39.         if (!flag) continue;
  40.         for (auto & p : fixed_r)
  41.             if (col == p.second) {
  42.                 flag = false;
  43.                 break;
  44.             }
  45.         if (!flag) continue;
  46.         if (r > 0) {
  47.             fixed_r.push_back({row, col});
  48.             go(row+1, n, q, r-1, answer);
  49.             fixed_r.pop_back();
  50.         }
  51.         for (auto & p : fixed_r)
  52.             if (is_diagonal({row, col}, p) || p.second == col) {
  53.                 flag = false; break;
  54.             }
  55.         if (q > 0 && flag) {
  56.             fixed_q.push_back({row, col});
  57.             go(row+1, n, q-1, r, answer);
  58.             fixed_q.pop_back();
  59.         }
  60.     }
  61. }
  62.  
  63. int main() {
  64.     auto file = fopen("output10.txt", "wt");
  65.     for (int n = 10; n <= 10; ++n)
  66.     for (int q = 0; q <= n; ++q)
  67.     for (int r = 0; r <= n-q; ++r) {
  68.         int answer = 0;
  69.         go(0, n, q, r, answer);
  70.         fprintf(file, "answer[%d][%d][%d] = %d;\n", n, q, r, answer);
  71.         cout << "n = " << n << ", q = " << q << ", r = " << r << ", answer = " << answer << endl;
  72.     }
  73.     cout << "ok";
  74.     fclose(file);
  75.     return 0;
  76. }
Add Comment
Please, Sign In to add comment