Advertisement
Guest User

Untitled

a guest
Jul 21st, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.22 KB | None | 0 0
  1. ///====================== TEMPLATE STARTS HERE =====================///
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. //#include <ext/pb_ds/assoc_container.hpp> // Include for built in treap
  6. //#include <ext/pb_ds/tree_policy.hpp>
  7. //using namespace __gnu_pbds;
  8.  
  9. //#include <sys/resource.h>     // for linux stack memory increase
  10. //#define gc getchar_unlocked   // for linux fast io
  11. //#define gc getchar            // for windows fast io
  12.  
  13. #define pb push_back
  14. #define MP make_pair
  15. #define ff first
  16. #define ss second
  17. #define nl puts("")
  18. #define sp printf(" ")
  19. #define phl debug("Hello")
  20. #define FOR(i,x,y) for(vlong i = (x) ; i <= (y) ; ++i)
  21. #define ROF(i,x,y) for(vlong i = (y) ; i >= (x) ; --i)
  22. #define CLR(x,y) memset(x,y,sizeof(x))
  23. #define ALL(x) (x).begin(),(x).end()
  24. #define SZ(x) ((vlong)(x).size())
  25. #define UNIQUE(V) (V).erase(unique((V).begin(),(V).end()),(V).end())
  26. #define MIN(a,b) ((a)<(b)?(a):(b))
  27. #define MAX(a,b) ((a)>(b)?(a):(b))
  28. #define ABS(x) ((x)<0?-(x):(x))
  29. #define FABS(x) ((x)+eps<0?-(x):(x))
  30. #define SQ(x) ((x)*(x))
  31. #define LCM(x,y) (((x)/gcd((x),(y)))*(y))
  32. #define POPCOUNT __builtin_popcountll
  33. #define RIGHTMOST __builtin_ctzll
  34. #define LEFTMOST(x) (63-__builtin_clzll((x)))
  35. #define NUMDIGIT(x,y) (((vlong)(log10((x))/log10((y))))+1)
  36. #define NORM(x) if(x>=mod) x-=mod;if(x<0) x+=mod;
  37. #define ODD(x) (((x)&1)==0?(0):(1))
  38. #define Set(N,p) N=((N)|((1LL)<<(p)))
  39. #define Reset(N,p) N=((N)&(~((1LL)<<(p))))
  40. #define Check(N,p) (!(((N)&((1LL)<<(p)))==(0)))
  41. #define fast_cin ios_base::sync_with_stdio(false);cin.tie(NULL)
  42. #define arrayIndexPrint(A,i) cerr<<"~ At pos: "<<i<<" = "<<A[i]
  43. #define dump(x) cerr<<"~ "<<#x<<" = "<<x<<endl
  44. #define arrayPrint(A,st,ed) cerr<<"~ Array:";FOR(i,st,ed) cerr<<" "<<A[i];cerr<<endl
  45. #define LINE cerr<<"\n"; FOR(i,0,50) cerr<<"=";cerr<<"\n\n"
  46.  
  47. #define LL long long
  48. #define LLU long long unsigned int
  49. typedef long long vlong;
  50. typedef unsigned long long uvlong;
  51. typedef pair < int, int > pii;
  52. typedef pair < vlong, vlong > pll;
  53. typedef vector<int> vi;
  54. typedef vector<vlong> vl;
  55. typedef vector<pll> vll;
  56. //typedef tree <int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pb_ds;
  57.  
  58. #ifdef forthright48
  59.      #include <ctime>
  60.      clock_t tStart = clock();
  61.      #define debug(args...) {dbg,args; cerr<<endl;}
  62.      #define timeStamp debug ("Execution Time: ", (double)(clock() - tStart)/CLOCKS_PER_SEC)
  63.      #define bug printf("%d\n",__LINE__);
  64.  
  65. #else
  66.     #define debug(args...)  // Just strip off all debug tokens
  67.     #define timeStamp
  68. #endif
  69.  
  70. struct debugger{
  71.     template<typename T> debugger& operator , (const T& v){
  72.         cerr<<v<<" ";
  73.         return *this;
  74.     }
  75. }dbg;
  76.  
  77. inline vlong gcd ( vlong a, vlong b ) {
  78.     a = ABS ( a ); b = ABS ( b );
  79.     while ( b ) { a = a % b; swap ( a, b ); } return a;
  80. }
  81.  
  82. vlong ext_gcd ( vlong A, vlong B, vlong *X, vlong *Y ){
  83.     vlong x2, y2, x1, y1, x, y, r2, r1, q, r;
  84.     x2 = 1; y2 = 0;
  85.     x1 = 0; y1 = 1;
  86.     for (r2 = A, r1 = B; r1 != 0; r2 = r1, r1 = r, x2 = x1, y2 = y1, x1 = x, y1 = y ) {
  87.         q = r2 / r1;
  88.         r = r2 % r1;
  89.         x = x2 - (q * x1);
  90.         y = y2 - (q * y1);
  91.     }
  92.     *X = x2; *Y = y2;
  93.     return r2;
  94. }
  95.  
  96. inline vlong modInv ( vlong a, vlong m ) {
  97.     vlong x, y;
  98.     ext_gcd( a, m, &x, &y );
  99.     x %= m;
  100.     if ( x < 0 ) x += m;
  101.     return x;
  102. }
  103.  
  104. inline vlong bigmod ( vlong a, vlong p, vlong m ) {
  105.     vlong res = 1 % m, x = a % m;
  106.     while ( p ) {
  107.         if ( p & 1 ) res = ( res * x ) % m;
  108.         x = ( x * x ) % m; p >>= 1; /// For bigmod2 multiply here using mulmod
  109.     }
  110.     return res;
  111. }
  112.  
  113.  
  114. //int knightDir[8][2] = { {-2,1},{-1,2},{1,2},{2,1},{2,-1},{-1,-2},{1,-2},{-2,-1} };
  115. //int dir4[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};
  116. //int dir8[8][2] = {{-1,0},{0,1},{1,0},{0,-1},{-1,-1},{1,1},{1,-1},{-1,1}};
  117. const vlong inf = 2147383;
  118. const vlong mod = 1000000007;
  119. const double pi = 2 * acos ( 0.0 );
  120. const double eps = 1e-11;
  121.  
  122. ///======================  TEMPLATE ENDS HERE  =====================///
  123.  
  124. struct chess{
  125.     int row[8], col[8];
  126. };
  127.  
  128. vector<chess> List; /// List of all valid chess states
  129. string mat[8];
  130.  
  131. /// Check if i place a queen at (r,c) then will it clash
  132. /// with previous queens.
  133. bool clash(chess &cur, int r, int c){
  134.     FOR(i,0,r-1){
  135.         if(cur.col[i] == c) return true;
  136.         if(ABS(r-cur.row[i]) == ABS(c-cur.col[i])) return true;
  137.     }
  138.     return false;
  139. }
  140.  
  141. /// Find all possible valid states
  142. void backtrack(int r, chess &cur){
  143.     if(r == 8){
  144.         List.pb(cur);
  145.         return;
  146.     }
  147.     FOR(c,0,7){
  148.         if(clash(cur, r, c)) continue;
  149.         cur.row[r] = r;
  150.         cur.col[r] = c;
  151.         backtrack(r+1, cur);
  152.     }
  153. }
  154.  
  155. int DP[(1<<8)+50];
  156.  
  157. int moveCount(chess &src, int q1, chess &trg, int q2){
  158.     int r1 = src.row[q1], c1 = src.col[q1];
  159.     int r2 = trg.row[q2], c2 = trg.col[q2];
  160.     if(r1==r2 && c1==c2) return 0; /// Same cell
  161.     if(r1==r2 || c1 == c2) return 1; /// Same row/col
  162.     if(ABS(r1-r2) == ABS(c1-c2)) return 1; /// Same diagonal
  163.     return 2;
  164. }
  165.  
  166. int call(int mask, chess &src, chess &trg){
  167.     int q1 = POPCOUNT(mask);
  168.     if(q1 == 8) return 0;
  169.     if(DP[mask] != -1) return DP[mask];
  170.  
  171.     int res = inf;
  172.     FOR(q2,0,7){
  173.         if(Check(mask, q2)) continue;
  174.         int nmask = mask;
  175.         Set(nmask, q2);
  176.         int ret = call(nmask, src, trg) + moveCount(src, q1, trg, q2);
  177.         res = min(res, ret);
  178.     }
  179.     return DP[mask] = res;
  180. }
  181.  
  182. chess buildChess(){
  183.     chess cur;
  184.     int q = 0;
  185.     FOR(r,0,7){
  186.         FOR(c,0,7){
  187.             if(mat[r][c] == 'q'){
  188.                 cur.row[q] = r, cur.col[q] = c;
  189.                 q++;
  190.             }
  191.         }
  192.     }
  193.     return cur;
  194. }
  195.  
  196. int main () {
  197.     #ifdef forthright48
  198.     freopen ( "input.txt", "r", stdin );
  199.     //freopen ( "output.txt", "w", stdout );
  200.     #endif // forthright48
  201.  
  202.     fast_cin;
  203.  
  204.     chess cur;
  205.     backtrack(0, cur);
  206.  
  207.     int t;
  208.     cin >> t;
  209.     FOR(cs,1,t){
  210.         FOR(i,0,7){
  211.             cin >> mat[i];
  212.         }
  213.         chess src = buildChess();
  214.         int res = inf;
  215.         FOR(i,0,SZ(List)-1){
  216.             CLR(DP, -1);
  217.             int ret = call(0, src, List[i]);
  218.             res = min(res, ret);
  219.         }
  220.         printf("Case %lld: %d\n",cs,res);
  221.     }
  222.  
  223.     return 0;
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement