From 20f2c97aad056132bc8abb1666a0587405ab0dd2 Mon Sep 17 00:00:00 2001 From: Haripriya <93302077+Haripriya-1212@users.noreply.github.com> Date: Fri, 6 Oct 2023 10:40:52 +0530 Subject: [PATCH] Create nQueens.cpp n queens problem with steps --- backtracking/nQueens.cpp | 124 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 backtracking/nQueens.cpp diff --git a/backtracking/nQueens.cpp b/backtracking/nQueens.cpp new file mode 100644 index 000000000..01b6a3326 --- /dev/null +++ b/backtracking/nQueens.cpp @@ -0,0 +1,124 @@ +#include +using namespace std; + +bool isSafe(int arr[][4],int x,int y,int n){ + + // in column + for(int i=0;i=0 && col>=0){ + if(arr[row][col]==1){ + return false; + } + row--; + col--; + } + + row = x; + col = y; + + + // right diagonal + while(row>=0 && col n ==> done + if(row>=n){ + return true; + } + + // columns - elements in a particular row + for(int j=0;j