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

Exp 12.1

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

Exp 12.1

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:43 PM Microsoft Copilot: Your AI companion

Sure! Let's go through the provided code line by line to understand its functionality. This code implements a solution to the N-Queens problem using backtracking.

Cpp Copy

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

This includes all standard C++ libraries and declares the std namespace, making standard functions and objects accessible without prefixing them with std:: .

Cpp Copy

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

This function checks if placing a queen at position (x, y) is safe.

Cpp Copy

for (int row = 0; row < x; row++) {


if (arr[row][y] == 1) {
return false;
}
}

Checks the current column (y) for any queens from row 0 to x-1. If a queen is found ( arr[row][y] == 1 ), it returns false.

Cpp Copy

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

Checks the upper left diagonal for any queens. If a queen is found, it returns false.

Cpp Copy

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

Checks the upper right diagonal for any queens. If a queen is found, it returns false.

Cpp Copy

return true;
}

If no queens are found in the same column or either diagonal, it returns true, indicating it is safe to place a queen at (x, y).

Cpp Copy

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

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


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

This function attempts to place queens row by row. If all rows are processed ( x >= n ), it returns true, indicating a solution is found.

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;
}

For each column in the current row (x), it checks if placing a queen there is safe using issafe .
If safe, it places a queen ( arr[x][col] = 1 ) and recursively attempts to place queens in the next row ( nqueen(arr, x + 1, n) ).
If placing the next queen fails, it removes the queen ( arr[x][col] = 0 ) and tries the next column.
If no columns work for the current row, it returns false.

Cpp Copy

int main() {
int n;
cin >> n;

The main function reads the size of the board (n).

Cpp Copy

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;
}
}

Dynamically allocates a 2D array to represent the n x n chessboard and initializes all positions to 0 (no queens).

Cpp Copy

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;
}
}
}

Calls the nqueen function to solve the N-Queens problem starting from row 0.
If a solution is found, it prints the board configuration where 1 represents a queen and 0 represents an empty space.

Summary:

https://copilot.microsoft.com/chats/jaJLdyuFNg6syDzKfN7EE 2/3
11/6/24, 1:43 PM Microsoft Copilot: Your AI companion
This code solves the N-
Queens problem using a backtracking algorithm. The issafe function checks if a position is safe for placing a queen, and the nqueen function
1 2 3

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

You might also like