Guest User

Untitled

a guest
Mar 25th, 2021
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.73 KB | None | 0 0
  1. class Solution {
  2.     bool good(int a, int b, int dist) {
  3.         return a != b && abs(a - b) != dist;
  4.     }
  5.     vector<char> columnTaken, diagTaken, diagTaken2;
  6.     void rec(int row, int n, vector<vector<string>>& solutions, vector<string>& grid) {
  7.         if(row == n) {
  8.             solutions.push_back(grid);
  9.             return;
  10.         }
  11.         for(int col = 0; col < n; ++col) {
  12.             char& a = columnTaken[col];
  13.             char& b = diagTaken[col+row];
  14.             char& c = diagTaken2[col-row+n-1];
  15.             if(!a && !b && !c) {
  16.                 a = b = c = true;
  17.                 grid[row][col] = 'Q';
  18.                 rec(row + 1, n, solutions, grid);
  19.                 grid[row][col] = '.';
  20.                 a = b = c = false;
  21.             }
  22.         }
  23.     }
  24. public:
  25.     vector<vector<string>> solveNQueens(int n) {
  26.         if(n == 5) {
  27.             vector<vector<string>> solutions;
  28.             for(int a = 1; a <= n; ++a) {
  29.                 for(int b = 1; b <= n; ++b) {
  30.                     if(good(a, b, 1)) {
  31.                         for(int c = 1; c <= n; ++c) {
  32.                             if(good(a, c, 2) && good(b, c, 1)) {
  33.                                 for(int d = 1; d <= n; ++d) {
  34.                                     if(good(a, d, 3) && good(b, d, 2) && good(c, d, 1)) {
  35.                                         for(int e = 1; e <= n; ++e) {
  36.                                             if(good(a, e, 4) && good(b, e, 3) && good(c, e, 2) && good(d, e, 1)) {
  37.                                                 string A = ".....";
  38.                                                 string B = A;
  39.                                                 string C = B;
  40.                                                 string D = C;
  41.                                                 string E = D;
  42.                                                 A[a-1] = 'Q';
  43.                                                 B[b-1] = 'Q';
  44.                                                 C[c-1] = 'Q';
  45.                                                 D[d-1] = 'Q';
  46.                                                 E[e-1] = 'Q';
  47.                                                 solutions.push_back({A, B, C, D, E});
  48.                                             }
  49.                                         }
  50.                                     }
  51.                                 }
  52.                             }
  53.                         }
  54.                     }
  55.                 }
  56.             }
  57.             return solutions;
  58.         }
  59.         columnTaken = diagTaken = diagTaken2 = vector<char>(2 * n, 0);
  60.         vector<vector<string>> solutions;
  61.         vector<string> grid(n, string(n, '.'));
  62.         rec(0, n, solutions, grid);
  63.         return solutions;
  64.     }
  65. };
Advertisement
Add Comment
Please, Sign In to add comment