提交时间:2022-08-03 18:59:23

运行 ID: 57105

#include<bits/stdc++.h> using namespace std; int a,b[510][510],c,d[510],e[5]={0,0,1,0,-1},f[5]={0,1,0,-1,0}; string g; void bfs(int h,int k){ queue< pair<int,int> >p; p.push(make_pair(h,k)); b[h][k]=0; while(!p.empty()){ int l=p.front().first; int m=p.front().second; p.pop(); for(int i=0;i<4;i++){ int n=l+e[i]; int o=m+f[i]; if(n>=1&&n<=a&&o>=1&&o<=d[n]&&b[n][o]==1){ b[n][o]=0; p.push(make_pair(n,o)); } } } } int main(){ cin>>a; getline(cin,g); for(int i=1;i<=a;i++){ getline(cin,g); d[i]=g.size(); for(int j=1;j<=d[i];j++){ if(g[j-1]>='a'&&g[j-1]<='z') b[i][j]=1; } } for(int i=1;i<=a;i++){ for(int j=1;j<=d[i];j++){ if(b[i][j]==1){ bfs(i,j); c++; } } } cout<<c<<endl; return 0; }