xibao
原题链接:60966.WA聊天室2025-07-29 15:26:19
发布于:河北
#include <bits/stdc++.h>
using namespace std;
const int maxn=1100;
int n,m;
char mp[maxn][maxn];
int vis[maxn][maxn];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
void dfs(int x,int y){
for (int i=0;i<4;i++){
int tx=x+dx[i];
int ty=y+dy[i];
if (tx<=n&&tx>=1&&ty<=m&&ty>=1&&vis[tx][ty]==0&&mp[tx][ty]!='0'){
vis[tx][ty]=1;
dfs(tx,ty);
}
}
}
int main(){
cin >> n >> m;
for (int i=1;i<=n;i++){
for (int j=1;j<=m;j++){
cin >> mp[i][j];
}
}
int cnt=0;
for (int i=1;i<=n;i++){
for (int j=1;j<=m;j++){
if (vis[i][j]==1||mp[i][j]=='0')continue;
vis[i][j]=1;
dfs(i,j);
cnt++;
}
}
cout << cnt;
return 0;
}
这里空空如也
有帮助,赞一个