Ankit_132

D

Jul 22nd, 2023 (edited)
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.43 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define ll     long long
  6. #define _test   int _TEST; cin>>_TEST; while(_TEST--)
  7. #define ff     first
  8. #define ss     second
  9. #define pb     push_back
  10. #define ppb    pop_back
  11.  
  12. int main()
  13. {
  14.     int n, m;
  15.     cin>>n>>m;
  16.  
  17.     vector<string> s(n);
  18.     for(auto &e: s)     cin>>e;
  19.  
  20.     vector<vector<int>> done(n, vector<int> (m));
  21.     vector<vector<int>> vis(n, vector<int> (m));
  22.     done[1][1] = 1;
  23.     vis[1][1] = 1;
  24.     queue<pair<int, int>> qp;
  25.     qp.push({1, 1});
  26.  
  27.     auto func = [&](int i, int j)
  28.     {
  29.         int x, y;
  30.  
  31.         x = i+1, y = j;
  32.         while(s[x][y] == '.')   done[x++][y] = 1;
  33.         if(!vis[x-1][y])        qp.push({x-1, y}), vis[x-1][y] = 1;
  34.  
  35.         x = i-1, y = j;
  36.         while(s[x][y] == '.')   done[x--][y] = 1;
  37.         if(!vis[x+1][y])        qp.push({x+1, y}), vis[x+1][y] = 1;
  38.  
  39.         y = j+1, x = i;
  40.         while(s[x][y] == '.')   done[x][y++] = 1;
  41.         if(!vis[x][y-1])        qp.push({x, y-1}), vis[x][y-1] = 1;
  42.  
  43.         y = j-1, x = i;
  44.         while(s[x][y] == '.')   done[x][y--] = 1;
  45.         if(!vis[x][y+1])        qp.push({x, y+1}), vis[x][y+1] = 1;
  46.     };
  47.  
  48.     while(qp.size())
  49.     {
  50.         auto [i, j] = qp.front();
  51.         qp.pop();
  52.  
  53.         func(i, j);
  54.     }
  55.  
  56.     int ans = 0;
  57.  
  58.     for(int i=0; i<n; i++)
  59.     {
  60.         for(int j=0; j<m; j++)
  61.             ans += (done[i][j] == 1);
  62.     }
  63.  
  64.     cout<<ans<<"\n";
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment