Advertisement
Rana_093

BitmaskDPinGrid

Nov 3rd, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.41 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define ll long long int
  6. #define db(x) cout<<#x<<" -> "<<x<<endl
  7. #define inf (1<<29 )
  8.  
  9. int mask[12];
  10. int r,c;
  11. ll dp[10][(1<<8)+ 10  ] [(1<<8  )+ 10  ] ;
  12.  
  13. ll solve (int idx, int previous_mask, int current_mask)
  14. {
  15.     //printf("%d %d %d\n",idx,previous_mask,current_mask);
  16.     if(idx>=r)
  17.     {
  18.         // cout<<"Here"<<endl;
  19.         if( __builtin_popcount(previous_mask)==c  )  return 0LL;
  20.         else     return inf;
  21.     }
  22. //  if(idx>1 &&( previous_mask != ((1<<c)- 1  ) ))
  23. //   {
  24. //  cout<<"Here"<<endl;
  25. //        return inf;
  26. //    }
  27.     if(dp[idx][previous_mask][current_mask]!=-1)
  28.     {
  29.         return dp[idx][previous_mask][current_mask] ;
  30.     }
  31.     ll ret = inf;
  32.     for(int i=0 ;  i <(1<<c  );  i++ )
  33.     {
  34.         int next = mask[idx+1];
  35.         int prev = previous_mask;
  36.         int cur = current_mask;
  37.         int cnt = 0;
  38.         for(int j=0; j<c; j++ )
  39.         {
  40.             if(( i &(1<< j  )) ==0   )
  41.             {
  42.                 cnt++;
  43.                 cur^=(1<<j );
  44.                 next^=(1<<j );
  45.                 prev^=(1<<j );
  46.                 if(j-1>=0)
  47.                 {
  48.                     cur^=(1<<(j-1));
  49.                     next^=(1<<(j-1));
  50.                     prev^=(1<<(j-1));
  51.                 }
  52.                 if(j+1<c)
  53.                 {
  54.                     cur^=(1<<(j+1));
  55.                     next^=(1<<(j+1));
  56.                     prev^=(1<<(j+1));
  57.                 }
  58.             }
  59.         }
  60.         if( idx ==0   ||   __builtin_popcount(prev )==c)
  61.         {
  62.             ret = min(ret,cnt+solve(idx+1,cur,next ));
  63.         }
  64.     }
  65.     return dp[idx][previous_mask][current_mask] = ret;
  66. }
  67.  
  68. int main()
  69. {
  70.     ios_base::sync_with_stdio(false);
  71.     cin.tie(0);
  72.     int t;
  73.     cin>>t;
  74.     int tc = 0;
  75.     while(t--)
  76.     {
  77.         cin>>r>>c;
  78.         memset(mask,0,sizeof(mask));
  79.         memset(dp,-1,sizeof(dp));
  80.         for(int i=0; i<r; i++)
  81.         {
  82.             for(int j=0; j<c; j++)
  83.             {
  84.                 char ch;
  85.                 cin>>ch;
  86.                 if(ch=='*')
  87.                 {
  88.                     mask[i]|=(1<<j );
  89.                 }
  90.             }
  91.         }
  92.         ll ans = solve(0,0,mask[0]);
  93.         if(ans>=inf)
  94.         {
  95.             printf("Case %d: impossible\n",++tc);
  96.             continue;
  97.         }
  98.         printf("Case %d: %lld\n",++tc,ans);
  99.     }
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement