Advertisement
Fastrail08

N Queens

Jun 3rd, 2025
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.90 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. bool isCellSafe(int row, int col, vector<vector<bool> > &chess){
  5.     // check column (|), if the current queen is able to kill a queen in the same column or not
  6.     for(int i = 0; i < row; i++){
  7.         if(chess[i][col]){
  8.             return false;
  9.         }
  10.     }
  11.    
  12.     // check upper left diagonal (\), if the current queen is able to kill a previous queen in the left diagonal or not
  13.     for(int i = row - 1, j = col - 1; (i >= 0 && j >= 0); i--, j--){
  14.         if(chess[i][j]){
  15.             return false;
  16.         }
  17.     }
  18.    
  19.     //check upper right diagonal (/), if the current queen is able to kill a previous queen in the right diagonal or not
  20.     for(int i = row - 1, j = col + 1; (i >= 0 && j < chess[0].size()); i--, j++){
  21.         if(chess[i][j]){
  22.             return false;
  23.         }
  24.     }
  25.    
  26.     // currently placed queen would not be able to kill any queen placed before it, so safe to place the queen there
  27.     return true;
  28. }
  29.  
  30. void placeNQueens(int row, vector<vector<bool> > &chess, vector<string> &configurations){
  31.     //base case (print any configuration that reaaches base case, as we would only reach base case, if we are able to place all the N queens in N rows, as we have explored ONLY VALID options at each row, and we have made the decisions for all N rows, if our current state/row = N [we started from row 0, row 1 ...... row (n - 1) = N rows])
  32.     if(row == chess.size()){
  33.         string validConfig = "";
  34.         for(int i = 0; i < chess.size(); i++){
  35.             for(int j = 0; j < chess[0].size(); j++){
  36.                 if(chess[i][j]){
  37.                     // cout << '[' << i << ']' << '[' << j << ']' << '-';
  38.                     validConfig += "[" + to_string(i) + "]" + "[" + to_string(j) + "]" + "-";
  39.                 }
  40.             }
  41.         }
  42.         // cout << '\n';
  43.         configurations.push_back(validConfig);
  44.         return;
  45.     }
  46.     /*
  47.     level/state = row
  48.     options/transition = columns
  49.     */
  50.     //explore all the options in each row (queen sits in which column for a given row r)
  51.     for(int col = 0; col < chess[row].size(); col++){
  52.         // we will place the current queen in column col, only if it does not kill any queen placed before it in row - 1 rows
  53.         if(isCellSafe(row, col, chess)){
  54.             chess[row][col] = true;
  55.             placeNQueens(row + 1, chess, configurations);
  56.             chess[row][col] = false;
  57.         }
  58.     }
  59. }
  60.  
  61. int main() {
  62.     // your code goes here
  63.     int n;
  64.     cin >> n;
  65.    
  66.     //chessboard n X n
  67.     vector<vector<bool> > chess(n, vector<bool>(n, false));
  68.    
  69.     // Valid Positions
  70.     vector<string> configurations;
  71.     placeNQueens(0, chess, configurations);
  72.    
  73.     //print configurations
  74.     cout << "Total configurations: " << configurations.size() << ':' << '\n';
  75.     for(auto config : configurations){
  76.         cout << config << '\n';
  77.     }
  78. }
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement