fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const long long MaxN = 5e2 + 5;
  5.  
  6. long long n,m,ans=0;
  7. long long dx[4]={-1,0,1,0};
  8. long long dy[4]={0,1,0,-1};
  9.  
  10. bool visited[MaxN][MaxN];
  11. char arr[MaxN][MaxN];
  12.  
  13. void bfs(long long i,long long j)
  14. {
  15. queue<pair<long long,long long>> qu;
  16.  
  17. qu.push({i,j});
  18. visited[i][j]=true;
  19.  
  20. bool check=false;
  21. long long cnt=0;
  22.  
  23. while(!qu.empty())
  24. {
  25. long long x=qu.front().first;
  26. long long y=qu.front().second;
  27. qu.pop();
  28.  
  29. cnt++;
  30.  
  31. for(long long t=0;t<4;t++)
  32. {
  33. long long new_x=x+dx[t];
  34. long long new_y=y+dy[t];
  35.  
  36. if(new_x>n||new_x<1||new_y>m||new_y<1)
  37. {
  38. continue;
  39. }
  40.  
  41. if(arr[new_x][new_y]=='.')
  42. {
  43. check=true;
  44. }
  45.  
  46. if(arr[new_x][new_y]=='W'&&!visited[new_x][new_y])
  47. {
  48. visited[new_x][new_y]=true;
  49. qu.push({new_x,new_y});
  50. }
  51. }
  52. }
  53.  
  54. if(!check)
  55. {
  56. ans+=cnt;
  57. }
  58. }
  59.  
  60. void input()
  61. {
  62. cin>>n>>m;
  63.  
  64. for(long long i=1;i<=n;i++)
  65. {
  66. for(long long j=1;j<=m;j++)
  67. {
  68. cin>>arr[i][j];
  69. visited[i][j]=false;
  70. }
  71. }
  72. }
  73.  
  74. void solve()
  75. {
  76. for(long long i=1;i<=n;i++)
  77. {
  78. for(long long j=1;j<=m;j++)
  79. {
  80. if(arr[i][j]=='W'&&!visited[i][j])
  81. {
  82. bfs(i,j);
  83. }
  84. }
  85. }
  86.  
  87. cout<<ans;
  88. }
  89.  
  90. int main()
  91. {
  92. ios_base::sync_with_stdio(0);
  93. cin.tie(0);
  94.  
  95. input();
  96. solve();
  97.  
  98. return 0;
  99. }
Success #stdin #stdout 0s 5276KB
stdin
Standard input is empty
stdout
Standard output is empty