0% found this document useful (0 votes)
7 views

Exp 12.3

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Exp 12.3

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

11/6/24, 1:47 PM Microsoft Copilot: Your AI companion

Certainly! I'll go through each segment of the code in detail, explaining the causes or reasons behind each line and structure.

Including Libraries and Namespace

Cpp Copy

#include <bits/stdc++.h>
using namespace std;

Including Libraries: The #include <bits/stdc++.h> directive includes all standard C++ libraries. This is a commonly used shorthand in compe
Namespace: using namespace std; allows the use of standard library functions without prefixing std:: .

Function to Check if the Position is Safe

Cpp Copy

bool issafe(int** arr, int x, int y, int n) {


for (int row = 0; row < x; row++) {
if (arr[row][y] == 1) {
return false;
}
}

Checking Column: The loop for (int row = 0; row < x; row++) checks all rows up to the current row x in the column y . If any cell in this
[y] == 1 ), it's unsafe to place another queen in column y of the current row x .

Cause: This is to ensure that no two queens are placed in the same column, which would result in them attacking each other.

Cpp Copy

int row = x;
int col = y;
while (row >= 0 && col >= 0) {
if (arr[row][col] == 1) {
return false;
}
row--;
col--;
}

Checking Upper Left Diagonal: This loop checks if there is a queen in the upper left diagonal of the current position (x, y). It decrements both
left.
Cause: This is to ensure that no two queens are placed in the same diagonal, which would result in them attacking each other diagonally.

Cpp Copy

row = x;
col = y;
while (row >= 0 && col < n) {
if (arr[row][col] == 1) {
return false;
}
row--;
col++;
}
return true;
}

Checking Upper Right Diagonal: This loop checks if there is a queen in the upper right diagonal of the current position (x, y). It decrements ro
right.
Cause: This is to ensure that no two queens are placed in the same diagonal, maintaining the safety rule of no queens attacking each other diag

https://copilot.microsoft.com/chats/jaJLdyuFNg6syDzKfN7EE 1/3
11/6/24, 1:47 PM Microsoft Copilot: Your AI companion

Function for N-Queens Problem Using Backtracking

Cpp Copy

bool nqueen(int** arr, int x, int n) {


if (x >= n) {
return true;
}

Base Condition: if (x >= n) { return true; } checks if all rows have been processed. If so, a solution is found, and it returns true.
Cause: This indicates that queens have been successfully placed in all rows without conflicts.

Placing Queens Column by Column

Cpp Copy

for (int col = 0; col < n; col++) {


if (issafe(arr, x, col, n)) {
arr[x][col] = 1;
if (nqueen(arr, x + 1, n)) {
return true;
}
arr[x][col] = 0;
}
}
return false;
}

Column Loop: for (int col = 0; col < n; col++) iterates through each column of the current row x .
Safety Check: if (issafe(arr, x, col, n)) checks if placing a queen at (x, col) is safe.
Placing a Queen: If it's safe, arr[x][col] = 1 places a queen at (x, col).
Recursive Call: if (nqueen(arr, x + 1, n)) recursively attempts to place queens in the next row.
Backtracking: If placing a queen at (x, col) does not lead to a solution, arr[x][col] = 0 removes the queen and tries the next column.
Cause: The recursion and backtracking ensure that all possible configurations are tried until a valid solution is found.

Main Function

Cpp Copy

int main() {
int n;
cin >> n;
int** arr = new int*[n];
for (int i = 0; i < n; i++) {
arr[i] = new int[n];
for (int j = 0; j < n; j++) {
arr[i][j] = 0;
}
}

Input Size: cin >> n; reads the size of the chessboard.


Dynamic Allocation: int** arr = new int*[n]; allocates memory for an n x n chessboard.
Initialization: Nested loops initialize the board to 0, indicating no queens are placed initially.
Cause: Dynamic allocation and initialization prepare the chessboard for the N-Queens solution.

Solving the N-Queens Problem and Printing the Solution

Cpp Copy

https://copilot.microsoft.com/chats/jaJLdyuFNg6syDzKfN7EE 2/3
11/6/24, 1:47 PM Microsoft Copilot: Your AI companion

if (nqueen(arr, 0, n)) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
}
return 0;
}

Calling the Function: if (nqueen(arr, 0, n)) calls the nqueen function to solve the N-Queens problem starting from row 0.
Printing the Solution: Nested loops print the board configuration if a solution is found ( 1 represents a queen and 0 represents an empty sp
Cause: Displaying the solution allows verification that the algorithm has correctly placed the queens on the board.

Causes for the Implementation


Safety Checks: Ensuring no queens can attack each other, the issafe function checks columns and diagonals for existing queens.
Backtracking: The nqueen function uses recursion and backtracking to explore all possible configurations until a solution is found.
Dynamic Memory Allocation: Allocating memory for the chessboard allows handling various board sizes specified by user input.
Base Condition: The base condition in nqueen ensures termination of recursion once all queens are placed successfully.
Column Loop and Safety Checks: The column loop and safety checks in nqueen ensure that queens are placed only in safe positions, maintain

https://copilot.microsoft.com/chats/jaJLdyuFNg6syDzKfN7EE 3/3

You might also like