Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std ;
- vector<int> queen ;
- int n = 4 ;
- bool check(int level, int col)
- {
- for (int i = 0; i < level; i++)
- {
- int queenCol = queen[i];
- int queenRow = i;
- if ((col == queenCol) or (abs(level - queenRow) == abs(col - queenCol)))
- {
- return false;
- }
- }
- return true;
- }
- void printsolution()
- {
- for(int i = 0 ; i < n ; i++){
- for(int j = 0 ; j < n ; j++)
- cout << (queen[i] == j) << ' ';
- cout << endl ;
- }
- cout << endl << endl;
- }
- int solve(int level)
- {
- if(level == n)
- {
- // because we Have to Place 8 Queen [ NO OTHER CHOICE LEFT ] //
- // thus we must reach level == n ; thus returning 1 as -> ture --> possible
- printsolution();
- return 1 ;
- }
- int ans = 0 ;
- for(int c = 0 ; c < n ; c++)
- {
- if(check(level,c))
- {
- // place
- queen[level] = c ;
- // calculate
- ans += solve(level + 1) ;
- // revert back --> UNDO
- queen[level] = -1 ;
- }
- }
- return ans ;
- }
- int main()
- {
- cin >> n;
- queen = vector<int>(n,-1) ;
- cout << solve(0) << endl ;
- }
Advertisement
Add Comment
Please, Sign In to add comment