fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int MAXN = 507;
  4. //vector <int> a[MAXN];
  5. int n, m, cnt, ans;
  6. bool mark[MAXN][MAXN];
  7. char a[MAXN][MAXN];
  8. queue <pair<int, int>> q;
  9. vector <pair <int, int>> save;
  10. int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
  11.  
  12. bool check(int x, int y){return(x >= 1 and y >= 1 and y <= m and x <= n and !mark[x][y]);}
  13.  
  14. void bfs(int i, int j){
  15. mark[i][j] = 1;
  16. q.push({i, j});
  17. int cnt = 1;
  18. bool kt = false;
  19. while(!q.empty()){
  20. auto [x, y] = q.front();
  21. q.pop();
  22. for(int i = 0; i < 4; i++){
  23. int u = x + dx[i];
  24. int v = y + dy[i];
  25. if(check(u, v) and a[u][v] == '.') kt = 1;
  26. if(check(u, v) and a[u][v] == 'W'){
  27. mark[u][v] = 1;
  28. cnt++;
  29. q.push({u, v});
  30. }
  31. }
  32. }
  33. if(!kt) ans += cnt;
  34. }
  35.  
  36. int main(){
  37. ios_base::sync_with_stdio(0);
  38. cout.tie(0);
  39. cin.tie(0);
  40. cin >> n >> m;
  41. for(int i = 1; i <= n; i++){
  42. for(int j = 1; j <= m; j++){
  43. cin >> a[i][j];
  44. if(a[i][j] == 'W') save.push_back({i, j});
  45. }
  46. }
  47. for(auto [x, y] : save) if(!mark[x][y])bfs(x, y);
  48. cout << ans;
  49. }
Success #stdin #stdout 0s 5268KB
stdin
Standard input is empty
stdout
Standard output is empty