Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cmath>
- using namespace std;
- void escriure_tauler(const vector<int> &sp, int n)
- {
- vector<vector<char> > m(n, vector<char>(n, '.'));
- for (int i = 0; i < n; i++) {
- m[sp[i]][i] = 'Q';
- for (int j = 0; j < n; j++) {
- cout << m[j][i];
- }
- cout << endl;
- }
- cout << endl;
- }
- bool legal(const vector<int> &sp, int q, int n, int y,
- const vector<bool> &fila,
- const vector<bool> &diag_pos,
- const vector<bool> &diag_neg)
- {
- return not (fila[y] or diag_pos[q+y] or diag_neg[(n-y)+q-1]);
- }
- void reines(vector<int> &sp, int q, int n,
- vector<bool> &fila,
- vector<bool> &diag_pos,
- vector<bool> &diag_neg)
- {
- if (q == n) escriure_tauler(sp, n);
- else {
- for (int y = 0; y < n; y++) {
- if (legal(sp, q, n, y, fila, diag_pos, diag_neg)) {
- sp[q] = y;
- fila[y] = true;
- diag_pos[q+y] = true;
- diag_neg[(n-y)+q-1] = true;
- reines(sp, q+1, n, fila, diag_pos, diag_neg);
- diag_neg[(n-y)+q-1] = false;
- diag_pos[q+y] = false;
- fila[y] = false;
- sp[q] = -1;
- }
- }
- }
- }
- int main()
- {
- int n;
- cin >> n;
- vector<bool> fila(n, false),
- diag_neg(2*n-1, false),
- diag_pos(2*n-1, false);
- vector<int> sp(n, -1);
- reines(sp, 0, n, fila, diag_pos, diag_neg);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement