Advertisement
Abrar_Al_Samit

Red Polyomino (ABC 211 E) (AC)

Jul 24th, 2021
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.93 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4.  
  5. using namespace std;
  6. using namespace __gnu_pbds;
  7.  
  8.  
  9. #define debug(x) cerr << '[' << (#x) << "] = " << x << '\n';
  10.  
  11. template<class T> using ordered_set = tree<T, null_type , less<T> , rb_tree_tag , tree_order_statistics_node_update> ;
  12.  
  13. int n, k;
  14. char g[8][8];
  15. int dx[] {1, -1, 0, 0};
  16. int dy[] {0, 0, 1, -1};
  17. map<vector<pair<int,int>>,int>dp;
  18. vector<pair<int,int>>v;
  19. bool valid(int i, int j) {
  20.     return i>-1 && j>-1 && i<n && j<n && g[i][j] == '.';
  21. }
  22. void solve(int i, int j) {
  23.     g[i][j] = 'r';
  24.     v.push_back(make_pair(i, j));
  25.     vector<pair<int,int>> v2 = v;
  26.     sort(v2.begin(), v2.end());
  27.     if(dp.count(v2)) {
  28.         v.pop_back();
  29.         g[i][j] = '.';
  30.         return;
  31.     }
  32.     dp[v2] = 1;
  33.     if((int)v.size()==k) {
  34.         v.pop_back();
  35.         g[i][j] = '.';
  36.         return;
  37.     }
  38.     int sz = v.size();
  39.     for(int th=0; th<sz; ++th) {
  40.         int xx = v[th].first, yy = v[th].second;
  41.         for(int l=0; l<4; ++l) {
  42.             int nx = xx + dx[l], ny = yy + dy[l];
  43.             if(valid(nx, ny))
  44.                 solve(nx, ny);
  45.         }
  46.     }
  47.     v.pop_back();
  48.     g[i][j] = '.';
  49. }
  50. void PlayGround() {
  51.     cin >> n >> k;
  52.     for(int i=0; i<n; ++i) {
  53.         for(int j=0; j<n; ++j) {
  54.             cin >> g[i][j];
  55.         }
  56.     }
  57.     for(int i=0; i<n; ++i) {
  58.         for(int j=0; j<n; ++j) if(g[i][j]=='.') {
  59.             solve(i, j);
  60.         }
  61.     }
  62.     int ans = 0;
  63.     for(auto it : dp) {
  64.         ans += (int)it.first.size()==k;
  65.     }
  66.     cout << ans << '\n';
  67.  
  68.  
  69.     #ifndef ONLINE_JUDGE
  70.         cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
  71.     #endif
  72. }
  73. int main() {
  74.     ios_base::sync_with_stdio(0);
  75.     cin.tie(0);
  76.     cout.tie(0);
  77.     #ifndef ONLINE_JUDGE
  78.         freopen("input.txt", "r", stdin);
  79.     #endif
  80.     PlayGround();
  81.  
  82.     return 0;
  83. }  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement