Why?
原题链接:67636.细胞2025-08-20 14:22:14
发布于:广东
#include <bits/stdc++.h>
using namespace std;
int vis[105][105];
char mp[105][105];
struct nodint x,y;
};
int cnt=0;
int dx[4] = {-1,1,0,0};
int dy[4] = {0,0,-1,1};
int n,m,w;
void bfs(int x,int y){
queue<node> q;
q.push({x,y});
vis[x][y] = 1;
while(!q.empty()){
node now = q.front();
q.pop();
for(int i = 0;i < 4;i++){
int nx = now.x + dx[i];
int ny = now.y + dy[i];
if(mp[nx][ny] != '0' && nx >= 1 && nx <= n && ny >= 1 && ny <= m && vis[nx][ny] != true){
q.push({nx,ny});
vis[nx][ny] = 1;
}
}
}
}
int main(){
cin >> n >> m;
for(int i = 1;i <= n;i++){
for(int j = 1;j <= m;j++){
cin >> mp[i][j];
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (mp[i][j] != '0' && vis[i][j] == 0) {
bfs(i,j);
cnt++;
}
}
}
cout << cnt << endl;
return 0;
}
全部评论 1
怎么会WA,还只有一个测试点
2025-08-20 来自 广东
0
有帮助,赞一个