diff --git a/README.md b/README.md index e2760a9..b3b5859 100644 --- a/README.md +++ b/README.md @@ -151,6 +151,11 @@ This category should be reorganized. - number_theory/modular_arithmetics.cc - number_theory/primes.cc + +## Backtracking + +- n_queen_problem + ## Other This category should be reorganized. diff --git a/backtracking/n_queen_problem.cc b/backtracking/n_queen_problem.cc new file mode 100644 index 0000000..5b7e213 --- /dev/null +++ b/backtracking/n_queen_problem.cc @@ -0,0 +1,75 @@ +/* +** Provide the value of n +** This program will print one of the solution of N-Queen problem if possible for given n. +** N-Queen problem is a great example of backtracking. +** where you chose a path and if the solution is not found you backtrack to choose a different path. +*/ + +#include +using namespace std; +bool grid[10001][10001]; +int n; +bool isAttacked(int x, int y) +{ + for(int i = 0; i < n; i++) + { + if(grid[i][y] || grid[x][i]) + return true; + } + for(int i = x-1, j = y-1; i >= 0; i--, j--) + { + if(grid[i][j]) + return true; + } + for(int i = x-1, j = y+1; i >= 0; i--, j++) + { + if(grid[i][j]) + return true; + } + return false; +} +bool nQueen(int row) +{ + if(row >= n) + return true; + for(int i = 0; i < n; i++) + { + if(!isAttacked(row, i)) + { + grid[row][i] = true; + if(nQueen(row + 1)) + return true; + grid[row][i] = false; //BACKTRACKING + } + } + return false; +} +int main() +{ + cout<<"Enter number of queens : "; + cin>>n; + for(int i =0; i < n; i++) + for(int j = 0; j < n; j++) + grid[i][j] =false; + if(nQueen(0)) + { + cout<<"Solution :-\n"; + for(int i = 0; i < n; i++) + { + for(int j = 0; j < n; j++) + { + if(grid[i][j]) + cout<<"Q "; + else + cout<<"- "; + } + cout<