Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.11 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <algorithm>
  4. #include <string>
  5. #include <vector>
  6. #include <string.h>
  7. #include <set>
  8. #include <string>
  9. #include <memory>
  10. #include <iostream>
  11. #include <fstream>
  12. #include <sstream>
  13. #include <list>
  14. using namespace std;
  15.  
  16. typedef long long ll;
  17.  
  18. int n, m, X, r;
  19. vector < vector<vector<short>> > d;
  20. char a[25][25];
  21.  
  22. void calc (int x = 0, int y = 0, int mask = 0, int next_mask = 0, int cnt = 0, int next_cnt = 0)
  23. {
  24.     if (x == n)
  25.         return;
  26.     if (y >= m)
  27.         d[x+1][next_mask][next_cnt] = (d[x][mask][cnt] + d[x+1][next_mask][next_cnt]) % r;
  28.     else
  29.     {
  30.         int my_mask = 1 << y;
  31.         if (mask & my_mask)
  32.         {
  33. //            int next_cnt = cnt;
  34. //            if(x > 0 && a[x][y] == 'A' && a[x-1][y] == 'A')
  35. //                next_cnt++;
  36. //            next_cnt = min(next_cnt, X + 1);
  37.             calc (x, y+1, mask, next_mask, cnt, next_cnt);
  38.         }
  39.         else
  40.         {
  41.             int next_cnt2 = next_cnt;
  42.             if(x > 0 && a[x][y] == 'A' && a[x-1][y] == 'A')
  43.                 next_cnt2++;
  44.             next_cnt2 = min(next_cnt2, X + 1);
  45.             calc (x, y+1, mask, next_mask | my_mask, cnt, next_cnt2);
  46.             if (y+1 < m && ! (mask & my_mask) && ! (mask & (my_mask << 1)))
  47.             {
  48. //                next_cnt = cnt;
  49.                 if(a[x][y] == 'A' && a[x][y+1] == 'A')
  50.                     next_cnt++;
  51.                 next_cnt = min(next_cnt, X + 1);
  52.                 calc (x, y+2, mask, next_mask, cnt, next_cnt);
  53.             }
  54.         }
  55.     }
  56. }
  57.  
  58.  
  59. int main()
  60. {
  61.     int t;
  62.     cin >> t;
  63.     while(t--)
  64.     {
  65.         cin >> m >> n >> X >> r;
  66.         for(int i = 0; i < n; i++)
  67.             scanf("%s", a[i]);
  68.        
  69.         d.resize (n+1, vector<vector<short>> (1<<m, vector<short>(X+2)));
  70.         d[0][0][0] = 1;
  71.         for (int x=0; x<n; ++x)
  72.             for(int cnt = 0; cnt <= X; cnt++)
  73.                 for (int mask=0; mask<(1<<m); ++mask)
  74.                     calc (x, 0, mask, 0, cnt, 0);
  75.        
  76.         cout << d[n][0][X];
  77.     }
  78.    
  79. }
  80.  
  81. /*
  82. 3
  83. 4 3
  84. 5
  85. 1000
  86. AAAA
  87. ABBA
  88. AAAA
  89.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement