Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long
- #define _test int _TEST; cin>>_TEST; while(_TEST--)
- #define ff first
- #define ss second
- #define pb push_back
- #define ppb pop_back
- int main()
- {
- int n, m;
- cin>>n>>m;
- vector<string> s(n);
- for(auto &e: s) cin>>e;
- vector<vector<int>> done(n, vector<int> (m));
- vector<vector<int>> vis(n, vector<int> (m));
- done[1][1] = 1;
- vis[1][1] = 1;
- queue<pair<int, int>> qp;
- qp.push({1, 1});
- auto func = [&](int i, int j)
- {
- int x, y;
- x = i+1, y = j;
- while(s[x][y] == '.') done[x++][y] = 1;
- if(!vis[x-1][y]) qp.push({x-1, y}), vis[x-1][y] = 1;
- x = i-1, y = j;
- while(s[x][y] == '.') done[x--][y] = 1;
- if(!vis[x+1][y]) qp.push({x+1, y}), vis[x+1][y] = 1;
- y = j+1, x = i;
- while(s[x][y] == '.') done[x][y++] = 1;
- if(!vis[x][y-1]) qp.push({x, y-1}), vis[x][y-1] = 1;
- y = j-1, x = i;
- while(s[x][y] == '.') done[x][y--] = 1;
- if(!vis[x][y+1]) qp.push({x, y+1}), vis[x][y+1] = 1;
- };
- while(qp.size())
- {
- auto [i, j] = qp.front();
- qp.pop();
- func(i, j);
- }
- int ans = 0;
- for(int i=0; i<n; i++)
- {
- for(int j=0; j<m; j++)
- ans += (done[i][j] == 1);
- }
- cout<<ans<<"\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment