Question: This is the N Queen Problem Solution using Backtracking- #include #include using namespace std; void printArray(int n,int board[][20]){ for (int i = 0; i <
This is the N Queen Problem Solution using Backtracking-
#include
void printArray(int n,int board[][20]){ for (int i = 0; i < n; i++) { /* code */ for (int j = 0; j < n; j++) { cout< bool boardIsSafe(int board[][20],int n,int x,int y) { //checking the column for (int k = 0; k < x; k++) { if (board[k][y]==1) { return false; } } //checking the left diagonal int i = x; int j = y; while (i>=0 and j>=0) { /* code */ if (board[i][j]==1) { /* code */ return false; } i--,j--; } //checking right diagonal //your code is corrected here. //we have to initialize i & j again otherwise it will have -1 value for both i & j i=x; j=y; while (i>=0 and j void solveNqueen(int n,int board[][20],int i){ //base case if(i==n){ printArray(n,board); return ; } //rec case for (int j = 0; j < n; j++) { /* code */ if(boardIsSafe(board,n,i,j)){ board[i][j]=1; solveNqueen(n,board,i+1); //backtracking board[i][j]=0; } } return ; } int main(){ int board[20][20] = {0}; int n; cin>>n; solveNqueen(n,board,0); return 0; } You have to do(use above code)- 1.Explain the whole code and comment out in every section of the code(Easily Understandable) 2.System Design: Describe the algorithm of the project. 3. Implementation: Write and explain important parts of the code. Avoid explaining general parts such as declaration or initialization of variables. 4.Future Scope: Mention the limitations (if any) and future scope of your Project
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
