-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDP+BITMASK(BASIC).cpp
More file actions
44 lines (44 loc) · 1.19 KB
/
DP+BITMASK(BASIC).cpp
File metadata and controls
44 lines (44 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<bits/stdc++.h>
using namespace std;
int n;
int arr[22][22];
long long int dp[22][1050000];
long long int solve(int ind, int mask)
{
if(ind==n)
return 1;
if(dp[ind][mask]!=-1)
return dp[ind][mask];
long long int ans=0;
for(long long int i=0;i<n;i++)
{
if(arr[ind][i]==1)
{
if((mask&(1<<i))==0)
{
ans=ans+solve(ind+1,(mask|(1<<i)));
}
}
}
return dp[ind][mask]=ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(false);
int t;
cin>>t;
while(t--)
{
memset(dp,-1,sizeof(dp));
cin>>n;
for( int i=0;i<n;i++)
{
for( int j=0;j<n;j++)
cin>>arr[i][j];
}
long long int r=solve(0,0);
cout<<r<<"\n";
}
return 0;
}