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