Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cmath>
- #include <algorithm>
- using namespace std;
- const bool DEBUG = false;
- vector<pair<int, int>> fixed_q, fixed_r;
- bool is_diagonal(pair<int, int> a, pair<int, int> b) {
- return std::abs(a.first - b.first) == std::abs(a.second - b.second);
- }
- void go(int Row, int n, int q, int r, int& answer) {
- if (q + r == 0) {
- if (DEBUG) {
- printf("R:");
- for (auto & p : fixed_r) cout << " ("<<p.first<<","<<p.second<<")";
- printf("\n");
- printf("Q:");
- for (auto & p : fixed_q) cout << " ("<<p.first<<","<<p.second<<")";
- printf("\n");
- printf("\n");
- }
- ++answer;
- return;
- }
- if (Row == n) return;
- for (int row = Row; row <= n - q - r; ++row)
- for (int col = 0; col < n; ++col) {
- bool flag = true;
- for (auto & p : fixed_q)
- if (is_diagonal({row, col}, p) || col == p.second) {
- flag = false;
- break;
- }
- if (!flag) continue;
- for (auto & p : fixed_r)
- if (col == p.second) {
- flag = false;
- break;
- }
- if (!flag) continue;
- if (r > 0) {
- fixed_r.push_back({row, col});
- go(row+1, n, q, r-1, answer);
- fixed_r.pop_back();
- }
- for (auto & p : fixed_r)
- if (is_diagonal({row, col}, p) || p.second == col) {
- flag = false; break;
- }
- if (q > 0 && flag) {
- fixed_q.push_back({row, col});
- go(row+1, n, q-1, r, answer);
- fixed_q.pop_back();
- }
- }
- }
- int main() {
- auto file = fopen("output10.txt", "wt");
- for (int n = 10; n <= 10; ++n)
- for (int q = 0; q <= n; ++q)
- for (int r = 0; r <= n-q; ++r) {
- int answer = 0;
- go(0, n, q, r, answer);
- fprintf(file, "answer[%d][%d][%d] = %d;\n", n, q, r, answer);
- cout << "n = " << n << ", q = " << q << ", r = " << r << ", answer = " << answer << endl;
- }
- cout << "ok";
- fclose(file);
- return 0;
- }
Add Comment
Please, Sign In to add comment