Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //In the Name of Allah Most Gracious, Most Merciful//
- /*If you want something you've never had, you have to do something you never did.*/
- #include<bits/stdc++.h>
- using namespace std;
- #define pb push_back
- #define ll long long
- #define pii pair<int,int>
- #define pll pair<ll,ll>
- #define M 100007
- #define INF 1e9
- #define INFL 1e18
- #define PI acos(-1)
- #define mp make_pair
- //const int fx[]= {+1,-1,+0,+0};
- //const int fy[]= {+0,+0,+1,-1};
- //const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1}; // Kings Move
- //const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1}; // Kings Move
- //const int fx[]={-2, -2, -1, -1, 1, 1, 2, 2}; // Knights Move
- //const int fy[]={-1, 1, -2, 2, -2, 2, -1, 1}; // Knights Move
- int arr[18][18];
- int n;
- int dp[18][(1<<17)+10];
- ll mydp(int pos,int mask)
- {
- if(pos>=n)
- {
- return 0;
- }
- if(dp[pos][mask]!=-1)
- return dp[pos][mask];
- int ret=0;
- for(int i=0;i<n;i++)
- {
- int nmask=(mask & (1<<i));
- if(nmask!=0)
- continue;
- int setbit=mask |(1<<i);
- int x=arr[pos][i]+mydp(pos+1,setbit);
- // cout<< (mask|(1<<i)) <<endl;
- // cout<<x<<endl;
- ret=max(ret,x);
- }
- // cout<<ret<<endl;
- dp[pos][mask]=ret;
- }
- int main()
- {
- // #ifndef ONLINE_JUDGE
- // freopen("input.txt","r",stdin);
- // freopen("1011.txt","w",stdout);
- // #endif
- // cout<<(1<<16)<<endl;
- // return 0;
- int t,kase=0;
- cin>>t;
- while(t--)
- {
- cin>>n;
- for(int i=0;i<n;i++)
- {
- for(int j=0;j<n;j++)
- {
- cin>>arr[i][j];
- }
- }
- memset(dp,-1,sizeof dp);
- int ans=mydp(0,0);
- printf("Case %d: %d\n",kase+1,ans);
- kase++;
- //memset(arr,0,sizeof arr);
- }
- ///Before submit=>
- /// *check for integer overflow,array bounds
- /// *check for n=1
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement