Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.51 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int COUNT = 20;
  6. const int N = 55;
  7.  
  8. struct xorshift32 {
  9.   unsigned x,y,z,w;
  10.   xorshift32(): x(123456789), y(2378257), z(7777777), w(85715718) {}
  11.   xorshift32(unsigned seed): x(123456789), y(2378257), z(7777777), w(seed) {}
  12.   unsigned next() {
  13.     unsigned t=x^(x<<11);
  14.     x=y;y=z;z=w;
  15.     return w=w^(w>>19)^t^(t>>8);
  16.   }
  17.   unsigned next(unsigned n) {
  18.     return next()%n;
  19.   }
  20.   int next(int a, int b) {
  21.     return a+next(b-a+1);
  22.   }
  23. } rng;
  24.  
  25. const char SHAPE[COUNT][4][4] = {
  26.     {
  27.         { 1, 0, 0, 0 },
  28.         { 0, 0, 0, 0 },
  29.         { 0, 0, 0, 0 },
  30.         { 0, 0, 0, 0 }
  31.     },
  32.    
  33.     {
  34.         { 1, 0, 0, 0 },
  35.         { 1, 0, 0, 0 },
  36.         { 1, 0, 0, 0 },
  37.         { 1, 0, 0, 0 }
  38.     },
  39.    
  40.     {
  41.         { 1, 1, 1, 1 },
  42.         { 0, 0, 0, 0 },
  43.         { 0, 0, 0, 0 },
  44.         { 0, 0, 0, 0 }
  45.     },
  46.    
  47.     {
  48.         { 1, 1, 0, 0 },
  49.         { 1, 1, 0, 0 },
  50.         { 0, 0, 0, 0 },
  51.         { 0, 0, 0, 0 }
  52.     },
  53.    
  54.     {
  55.         { 1, 0, 0, 0 },
  56.         { 1, 1, 0, 0 },
  57.         { 1, 0, 0, 0 },
  58.         { 0, 0, 0, 0 }
  59.     },
  60.    
  61.     {
  62.         { 1, 1, 1, 0 },
  63.         { 0, 1, 0, 0 },
  64.         { 0, 0, 0, 0 },
  65.         { 0, 0, 0, 0 }
  66.     },
  67.    
  68.     {
  69.         { 0, 1, 0, 0 },
  70.         { 1, 1, 0, 0 },
  71.         { 0, 1, 0, 0 },
  72.         { 0, 0, 0, 0 }
  73.     },
  74.    
  75.     {
  76.         { 0, 1, 0, 0 },
  77.         { 1, 1, 1, 0 },
  78.         { 0, 0, 0, 0 },
  79.         { 0, 0, 0, 0 }
  80.     },
  81.    
  82.     {
  83.         { 1, 1, 0, 0 },
  84.         { 1, 0, 0, 0 },
  85.         { 1, 0, 0, 0 },
  86.         { 0, 0, 0, 0 }
  87.     },
  88.    
  89.     {
  90.         { 1, 1, 1, 0 },
  91.         { 0, 0, 1, 0 },
  92.         { 0, 0, 0, 0 },
  93.         { 0, 0, 0, 0 }
  94.     },
  95.    
  96.     {
  97.         { 0, 1, 0, 0 },
  98.         { 0, 1, 0, 0 },
  99.         { 1, 1, 0, 0 },
  100.         { 0, 0, 0, 0 }
  101.     },
  102.    
  103.     {
  104.         { 1, 0, 0, 0 },
  105.         { 1, 1, 1, 0 },
  106.         { 0, 0, 0, 0 },
  107.         { 0, 0, 0, 0 }
  108.     },
  109.    
  110.     {
  111.         { 1, 1, 0, 0 },
  112.         { 0, 1, 0, 0 },
  113.         { 0, 1, 0, 0 },
  114.         { 0, 0, 0, 0 }
  115.     },
  116.    
  117.     {
  118.         { 0, 0, 1, 0 },
  119.         { 1, 1, 1, 0 },
  120.         { 0, 0, 0, 0 },
  121.         { 0, 0, 0, 0 }
  122.     },
  123.    
  124.     {
  125.         { 1, 0, 0, 0 },
  126.         { 1, 0, 0, 0 },
  127.         { 1, 1, 0, 0 },
  128.         { 0, 0, 0, 0 }
  129.     },
  130.    
  131.     {
  132.         { 1, 1, 1, 0 },
  133.         { 1, 0, 0, 0 },
  134.         { 0, 0, 0, 0 },
  135.         { 0, 0, 0, 0 }
  136.     },
  137.    
  138.     {
  139.         { 1, 0, 0, 0 },
  140.         { 1, 1, 0, 0 },
  141.         { 0, 1, 0, 0 },
  142.         { 0, 0, 0, 0 }
  143.     },
  144.    
  145.     {
  146.         { 0, 1, 1, 0 },
  147.         { 1, 1, 0, 0 },
  148.         { 0, 0, 0, 0 },
  149.         { 0, 0, 0, 0 }
  150.     },
  151.    
  152.     {
  153.         { 1, 1, 0, 0 },
  154.         { 0, 1, 1, 0 },
  155.         { 0, 0, 0, 0 },
  156.         { 0, 0, 0, 0 }
  157.     },
  158.    
  159.     {
  160.         { 0, 1, 0, 0 },
  161.         { 1, 1, 0, 0 },
  162.         { 1, 0, 0, 0 },
  163.         { 0, 0, 0, 0 }
  164.     }
  165. };
  166.  
  167. const int TYPE[COUNT] = {
  168.     0,
  169.     1,
  170.     1,
  171.     2,
  172.     3,
  173.     3,
  174.     3,
  175.     3,
  176.     4,
  177.     4,
  178.     4,
  179.     4,
  180.     5,
  181.     5,
  182.     5,
  183.     5,
  184.     6,
  185.     6,
  186.     7,
  187.     7
  188. };
  189.  
  190. const int ROTATION[COUNT] = {
  191.     0,
  192.     0,
  193.     1,
  194.     0,
  195.     0,
  196.     1,
  197.     2,
  198.     3,
  199.     0,
  200.     1,
  201.     2,
  202.     3,
  203.     0,
  204.     1,
  205.     2,
  206.     3,
  207.     0,
  208.     1,
  209.     0,
  210.     1
  211. };
  212.  
  213. const pair < int, int > ANCHOR[COUNT] = {
  214.     make_pair(0,0),
  215.     make_pair(0,0),
  216.     make_pair(0,3),
  217.     make_pair(0,0),
  218.     make_pair(0,0),
  219.     make_pair(0,2),
  220.     make_pair(2,1),
  221.     make_pair(1,0),
  222.     make_pair(0,0),
  223.     make_pair(0,2),
  224.     make_pair(2,1),
  225.     make_pair(1,0),
  226.     make_pair(0,0),
  227.     make_pair(0,2),
  228.     make_pair(2,1),
  229.     make_pair(1,0),
  230.     make_pair(0,0),
  231.     make_pair(0,2),
  232.     make_pair(0,0),
  233.     make_pair(0,1)
  234. };
  235.  
  236. const pair < int, int > DIRECTIONS[4] = {
  237.     make_pair(1,0),
  238.     make_pair(-1,0),
  239.     make_pair(0,1),
  240.     make_pair(0,-1)
  241. };
  242.  
  243. int tests,current_case;
  244.  
  245. int n,m;
  246. char a[N][N];
  247. bool used[N][N];
  248. vector < pair < int, int > > places[COUNT];
  249. bool vis[N][N];
  250. int ans,shapes_count;
  251. int perm[N];
  252. int saved_ans,saved_shapes_count;
  253. vector < pair < int, int > > saved_places[COUNT];
  254.  
  255. bool can(int idx, int r, int c) {
  256.     if(SHAPE[idx][0][0]==1 && r==1 && c==1) return false;
  257.    
  258.     int i,j;
  259.    
  260.     for(i=0;i<4;i++) {
  261.         for(j=0;j<4;j++) {
  262.             if(SHAPE[idx][i][j]==1) {
  263.                 if(r+i>n || c+j>m) return false;
  264.                 if(used[r+i][c+j]) return false;
  265.             }
  266.         }
  267.     }
  268.    
  269.     return true;
  270. }
  271.  
  272. void place(int idx, int r, int c) {
  273.     int i,j;
  274.    
  275.     for(i=0;i<4;i++) {
  276.         for(j=0;j<4;j++) {
  277.             if(SHAPE[idx][i][j]==1) {
  278.                 used[r+i][c+j]=true;
  279.             }
  280.         }
  281.     }
  282.    
  283.     places[idx].push_back(make_pair(r,c));
  284.     ans+=(idx==0 ? 1 : 6);
  285.     ++shapes_count;
  286. }
  287.  
  288. void unplace(int idx, int r, int c) { //Assumes the last placed of that type was the current one
  289.     int i,j;
  290.    
  291.     for(i=0;i<4;i++) {
  292.         for(j=0;j<4;j++) {
  293.             if(SHAPE[idx][i][j]==1) {
  294.                 used[r+i][c+j]=false;
  295.             }
  296.         }
  297.     }
  298.    
  299.     places[idx].pop_back();
  300.     ans-=(idx==0 ? 1 : 6);
  301.     --shapes_count;
  302. }
  303.  
  304. void dfs(int r, int c) {
  305.     vis[r][c]=true;
  306.     if(used[r][c]) return;
  307.    
  308.     int i,p,t;
  309.    
  310.     for(i=0;i<4;i++) {
  311.         p=r+DIRECTIONS[i].first;
  312.         t=c+DIRECTIONS[i].second;
  313.        
  314.         if(p>=1 && p<=n && t>=1 && t<=m) if(!vis[p][t] && a[p][t]=='.') {
  315.             dfs(p,t);  
  316.         }
  317.     }
  318. }
  319.  
  320. bool check() {
  321.     int i,j,z,t;
  322.    
  323.     for(i=1;i<=n;i++) {
  324.         for(j=1;j<=m;j++) {
  325.             vis[i][j]=false;
  326.         }
  327.     }
  328.    
  329.     dfs(1,1);
  330.    
  331.     for(i=0;i<COUNT;i++) {
  332.         for(j=0;j<(int)(places[i].size());j++) {
  333.             bool has=false;
  334.            
  335.             for(z=0;z<4;z++) {
  336.                 for(t=0;t<4;t++) {
  337.                     if(SHAPE[i][z][t]==1) if(vis[z+places[i][j].first][t+places[i][j].second]) {
  338.                         has=true;
  339.                     }
  340.                 }
  341.             }
  342.            
  343.             if(has==false) return false;
  344.         }
  345.     }
  346.    
  347.     return true;
  348. }
  349.  
  350. void save_ans() {
  351.     saved_ans=ans;
  352.     saved_shapes_count=shapes_count;
  353.    
  354.     for(int i=0;i<COUNT;i++) {
  355.         saved_places[i]=places[i];
  356.     }
  357. }
  358.  
  359. void print_used() {
  360.     int i,j;
  361.  
  362.     for(i=1;i<=n;i++) {
  363.         for(j=1;j<=m;j++) {
  364.             if(used[i][j]) printf("1");
  365.             else printf("0");
  366.         }
  367.         printf("\n");
  368.     }
  369.     printf("---\n");
  370. }
  371.  
  372. int main() {
  373.     int i,j,z,it;
  374.    
  375.     for(i=0;i<COUNT-1;i++) {
  376.         perm[i]=i+1;
  377.     }
  378.    
  379.     scanf("%d", &tests);
  380.     for(current_case=1;current_case<=tests;current_case++) {
  381.         scanf("%d %d", &n, &m);
  382.         for(i=1;i<=n;i++) {
  383.             scanf("%s", a[i]+1);
  384.             for(j=1;j<=m;j++) {
  385.                 if(a[i][j]=='.') used[i][j]=false;
  386.                 else used[i][j]=true;
  387.             }
  388.         }
  389.        
  390.         saved_ans=0;
  391.        
  392.         for(it=1;it<=5;it++) {
  393.             for(i=0;i<COUNT-1;i++) {
  394.                 swap(perm[i],perm[rng.next(COUNT-1)]);
  395.             }
  396.        
  397.             for(i=1;i<=n;i++) {
  398.                 for(j=1;j<=m;j++) {
  399.                     if(a[i][j]=='.') used[i][j]=false;
  400.                     else used[i][j]=true;
  401.                 }
  402.             }
  403.        
  404.             ans=0;
  405.             shapes_count=0;
  406.             for(i=0;i<COUNT;i++) {
  407.                 places[i].clear();
  408.             }
  409.            
  410.             for(i=n;i>=1;i--) {
  411.                 for(j=m;j>=1;j--) {
  412.                     for(z=0;z<COUNT-1;z++) if(can(perm[z],i,j)) {
  413.                         place(perm[z],i,j);
  414.                         if(!check()) unplace(perm[z],i,j);
  415.                     }
  416.                 }
  417.             }
  418.            
  419.             for(i=n;i>=1;i--) {
  420.                 for(j=m;j>=1;j--) {
  421.                     if(can(0,i,j)) {
  422.                         place(0,i,j);
  423.                         if(!check()) unplace(0,i,j);
  424.                     }
  425.                 }
  426.             }
  427.            
  428.             if(ans>saved_ans) {
  429.                 save_ans();
  430.             }
  431.         }
  432.        
  433.         printf("%d %d\n", saved_shapes_count, saved_ans);
  434.         for(i=0;i<COUNT;i++) {
  435.             for(j=0;j<(int)(saved_places[i].size());j++) {
  436.                 printf("%d %d %d %d\n", saved_places[i][j].first+ANCHOR[i].first, saved_places[i][j].second+ANCHOR[i].second, TYPE[i], ROTATION[i]);
  437.             }
  438.         }
  439.     }
  440.    
  441.     return 0;
  442. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement