Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #include <algorithm>
- #include <string>
- #include <vector>
- #include <string.h>
- #include <set>
- #include <string>
- #include <memory>
- #include <iostream>
- #include <fstream>
- #include <sstream>
- #include <list>
- using namespace std;
- typedef long long ll;
- int n, m, X, r;
- vector < vector<vector<short>> > d;
- char a[25][25];
- void calc (int x = 0, int y = 0, int mask = 0, int next_mask = 0, int cnt = 0, int next_cnt = 0)
- {
- if (x == n)
- return;
- if (y >= m)
- d[x+1][next_mask][next_cnt] = (d[x][mask][cnt] + d[x+1][next_mask][next_cnt]) % r;
- else
- {
- int my_mask = 1 << y;
- if (mask & my_mask)
- {
- // int next_cnt = cnt;
- // if(x > 0 && a[x][y] == 'A' && a[x-1][y] == 'A')
- // next_cnt++;
- // next_cnt = min(next_cnt, X + 1);
- calc (x, y+1, mask, next_mask, cnt, next_cnt);
- }
- else
- {
- int next_cnt2 = next_cnt;
- if(x > 0 && a[x][y] == 'A' && a[x-1][y] == 'A')
- next_cnt2++;
- next_cnt2 = min(next_cnt2, X + 1);
- calc (x, y+1, mask, next_mask | my_mask, cnt, next_cnt2);
- if (y+1 < m && ! (mask & my_mask) && ! (mask & (my_mask << 1)))
- {
- // next_cnt = cnt;
- if(a[x][y] == 'A' && a[x][y+1] == 'A')
- next_cnt++;
- next_cnt = min(next_cnt, X + 1);
- calc (x, y+2, mask, next_mask, cnt, next_cnt);
- }
- }
- }
- }
- int main()
- {
- int t;
- cin >> t;
- while(t--)
- {
- cin >> m >> n >> X >> r;
- for(int i = 0; i < n; i++)
- scanf("%s", a[i]);
- d.resize (n+1, vector<vector<short>> (1<<m, vector<short>(X+2)));
- d[0][0][0] = 1;
- for (int x=0; x<n; ++x)
- for(int cnt = 0; cnt <= X; cnt++)
- for (int mask=0; mask<(1<<m); ++mask)
- calc (x, 0, mask, 0, cnt, 0);
- cout << d[n][0][X];
- }
- }
- /*
- 3
- 4 3
- 5
- 1000
- AAAA
- ABBA
- AAAA
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement