Skip to content

Commit

Permalink
Merge pull request #4 from Rajeshjha586/master
Browse files Browse the repository at this point in the history
N_Queen.cpp
  • Loading branch information
SrGrace authored Oct 5, 2021
2 parents 252c624 + 7d506b7 commit 0c6b9b8
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions Backtracking/N_Queen.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include<iostream>
#include<vector>

using namespace std;

vector<bool> ROWS;
vector<bool> COLS;
vector<bool> DIAG;
vector<bool> ADIAG;
int N_Queen_Problem_Combination_06(int n, int m, int idx, int tnq, string ans)
{
if(tnq==0)
{
cout << ans << endl;
return 1;
}

int count=0;
for(int r=idx; r<n*m; r++)
{
int x = r / m;
int y = r % m;

if(!ROWS[x] && !COLS[y] && !DIAG[x+y] && !ADIAG[x-y+(m-1)])
{
ROWS[x]=true; COLS[y]=true; DIAG[x+y]=true; ADIAG[x-y+(m-1)]=true;

count += N_Queen_Problem_Combination_06(n, m, r+1, tnq-1, ans+"("+to_string(x)+","+to_string(y)+") ");

ROWS[x]=false; COLS[y]=false; DIAG[x+y]=false; ADIAG[x-y+(m-1)]=false;

}
}

return count;
}

void Questions_Set_01()
{
int n=10;
int m=10;
int tnq=10;

ROWS.resize(n, false);
COLS.resize(m, false);
DIAG.resize(n+m-1, false);
ADIAG.resize(n+m-1, false);

cout << N_Queen_Problem_Combination_06(n, m, 0, tnq, "") << endl;
}

void solve()
{
Questions_Set_01();
}

int main()
{
solve();
return 0;
}

0 comments on commit 0c6b9b8

Please sign in to comment.