Advertisement
volochai

B 787

Jan 7th, 2023
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.22 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define ll long long
  4. #define all(x) (x).begin(), (x).end()
  5. #define rall(x) (x).rbegin(), (x).rend()
  6. #define boost ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
  7. #define watch(x) cout << (#x) << " : " << x << '\n'
  8.  
  9. using namespace std;    
  10.  
  11. const int N = 2e3 + 128;
  12. char a[N][N];
  13. int n, m;
  14.  
  15. inline int decode(char x) {
  16.     switch (x) {
  17.     case 'W':
  18.         return 0;
  19.     case 'E':
  20.         return 1;
  21.     case 'N':
  22.         return 2;
  23.     case 'S':
  24.         return 3;
  25.     }
  26.     return -1;
  27. }
  28.  
  29. bool escaped[N][N];
  30. pair<int, int> closest[N][N][4];
  31.  
  32. bool can_delete(int i, int j) {
  33.     if (escaped[i][j])
  34.         return false;
  35.     auto [ci, cj] = closest[i][j][decode(a[i][j])];
  36.     return ci == -1 || cj == -1;
  37. }
  38.  
  39. void push_next(int i, int j, int a, int b) {
  40.     if (closest[i][j][a].first == -1 || closest[i][j][a].second == -1)
  41.         return;
  42.     closest[closest[i][j][a].first][closest[i][j][a].second][b] = closest[i][j][b];
  43. }
  44.  
  45. int ans = 0;
  46.  
  47. void apply_delete(int i, int j) {
  48.     if (escaped[i][j])
  49.         return;
  50.     ans++;
  51.     escaped[i][j] = true;
  52.     push_next(i, j, 0, 1);
  53.     push_next(i, j, 1, 0);
  54.     push_next(i, j, 2, 3);
  55.     push_next(i, j, 3, 2);
  56. }  
  57.  
  58. vector < pair<int, int> > find_nearest(int i, int j) {
  59.     vector < pair<int, int> > res;  
  60.     for (int d = 0; d < 4; d++) {
  61.         auto [ci, cj] = closest[i][j][d];
  62.         if (ci != -1 && cj != -1)
  63.             res.push_back({ ci, cj });
  64.     }
  65.     return res;
  66. }
  67.  
  68. bool was[N][N];
  69.  
  70. void solve() {
  71.     cin >> n >> m;
  72.    
  73.     for (int i = 1; i <= n; i++)
  74.         for (int j = 1; j <= m; j++)
  75.             cin >> a[i][j];
  76.      
  77.     for (int i = 1; i <= n; i++) {
  78.         int prev = -1;
  79.         for (int j = 1; j <= m; j++) {
  80.             if (a[i][j] == '.')
  81.                 continue;
  82.             closest[i][j][0] = { i, prev };
  83.             prev = j;
  84.         }
  85.         prev = -1;
  86.         for (int j = m; j >= 1; j--) {
  87.             if (a[i][j] == '.')
  88.                 continue;
  89.             closest[i][j][1] = { i, prev };
  90.             prev = j;
  91.         }
  92.     }
  93.  
  94.     for (int j = 1; j <= m; j++) {
  95.         int prev = -1;
  96.         for (int i = 1; i <= n; i++) {
  97.             if (a[i][j] == '.')
  98.                 continue;
  99.             closest[i][j][2] = { prev, j };
  100.             prev = i;
  101.         }
  102.         prev = -1;
  103.         for (int i = n; i >= 1; i--) {
  104.             if (a[i][j] == '.')
  105.                 continue;
  106.             closest[i][j][3] = { prev, j };
  107.             prev = i;
  108.         }
  109.     }
  110.  
  111.     vector < pair<int, int> > can;
  112.     for (int i = 1; i <= n; i++)
  113.         for (int j = 1; j <= m; j++)
  114.             if (a[i][j] != '.' && can_delete(i, j))
  115.                 was[i][j] = true, can.push_back({ i, j });
  116.      
  117.     while (!can.empty()) {
  118.         for (auto [i, j] : can)
  119.             apply_delete(i, j);
  120.         vector < pair<int, int> > nxt;
  121.         for (auto [i, j] : can)
  122.             for (auto [x, y] : find_nearest(i, j))
  123.                 if (!was[x][y] && can_delete(x, y))
  124.                    was[x][y] = true, nxt.push_back({ x, y });
  125.         can = nxt;
  126.     }
  127.  
  128.     cout << ans << '\n';
  129. }
  130.  
  131. int32_t main() {
  132.     boost;    
  133.     solve();
  134.     return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement