1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| #include <bits/stdc++.h>
using namespace std; const int N = 2e5 + 10; const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3f; const unsigned long long P = 1e9 + 7; const double eps = 1e-6; char s[1005][1005]; bool vis[1005][1005]; int n,m; int MOVE[8][2] = {1,0,0,1,-1,0,0,-1,1,1,1,-1,-1,1,-1,-1}; bool isNoise(int x,int y){ for(int i = 0;i<8;i++){ int kx = x + MOVE[i][0]; int ky = y + MOVE[i][1]; if(s[kx][ky]=='#') return false; } return true; } void fill(int x,int y){ queue<pair<int,int>> q; q.push({x,y}); vis[x][y] = true; while(!q.empty()){ auto u = q.front(); q.pop(); for(int i = 0;i<8;i++){ int kx = u.first + MOVE[i][0]; int ky = u.second + MOVE[i][1]; if(kx<=n&&ky<=m&&kx>=1&&ky>=1&&!vis[kx][ky]&&s[kx][ky]=='#'){ vis[kx][ky] = true; q.push({kx,ky}); } } } } int check(int x,int y){ int rx = x,ry = y; while(s[rx][ry+1]=='#') ry++; while(s[rx+1][ry]=='#') rx++; while(s[x+1][y]=='#') x++; if(rx!=x) return 3; while(s[rx][ry-1]=='#') ry--; if(y!=ry) return 1; else return 2; } int main() { cin>>m>>n; for(int i = 1;i<=n;i++){ scanf("%s",s[i]+1); } fill(1,1); int ans1 = 0,ans2 = 0,ans3 = 0; for(int i = 1;i<=n;i++){ for(int j = 1;j<=m;j++){ if(!vis[i][j]&&s[i][j]=='#'){ if(isNoise(i,j)) vis[i][j] = true; else{ switch (check(i,j)) { case 1:ans1++;break; case 2:ans2++;break; case 3:ans3++;break; } fill(i,j); } } } } printf("%d %d %d",ans1,ans2,ans3); return 0; }
|