time limit exceeding code ide is https://ide.codingblocks.com/s/121603
Time Limit exceeding in the 1 test case
see below optimized sol.
#include<iostream>
#include<ctime>
#define DONE (1<<n)-1
using namespace std;
int ans =0;
void solve(int row,int ld,int rd,int n){
if(row==DONE){ ans++;return;}
int pos = DONE &(~(row|ld|rd));
while(pos>0){
int p = pos&(-pos);
pos -= p;
solve(row|p, (ld|p)<<1,(rd|p)>>1,n);
}
}
int main(){
int n;
cin>>n;
clock_t st = clock();
solve(0,0,0,n);
cout<<ans<<endl;
// cout<<ans<<endl;
return 0;
}
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.