Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<math.h>
- using namespace std;
- bool end = false;
- int temp;
- int board[20];
- int level = 1;
- bool valid;
- int n;
- foo(){
- if(!end){
- // cout<<"-----strt func-----"<<endl;
- valid = true;
- for(int i = 1; i <= n ; i++){
- //check if valid
- valid = true;
- {
- for(int ctr1=0; ctr1<level-1; ctr1++){
- if(board[ctr1]==i){
- valid = false;
- // cout<<board[ctr1]<<" "<<i<<endl<<"hori"<<endl;
- ctr1 = level;
- }
- if(abs(level-ctr1-1)==abs(i-board[ctr1])){
- valid = false;
- ctr1 = level;
- // cout<<"diag"<<endl;
- }
- }
- }
- if(valid){
- board[level-1]=i;
- if(level==n){
- for(int ctr1=0 ; ctr1<n ; ctr1++){
- cout<<board[ctr1]<<" ";
- }
- end = true;
- }
- level++;
- // cout<<"----- so far -----"<<endl;
- // for(int ctr1=0 ; ctr1<4 ; ctr1++){
- // cout<<board[ctr1];
- // }
- // cout<<endl<<"----- valid -----"<<endl;
- foo();
- level--;
- }
- if(!end){
- // cout<<"level = " << level <<endl;
- // cout<<"i = "<<i<<endl;
- // cout<<"-----end func-----"<<endl<<endl;
- }
- }
- }
- }
- int main(){
- cin >> n;
- foo();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement