社区讨论
0分求调 QWQ
P1451求细胞数量参与者 2已保存回复 1
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 1 条
- 当前快照
- 1 份
- 快照标识符
- @lyxsh5i6
- 此快照首次捕获于
- 2024/07/23 10:20 2 年前
- 此快照最后确认于
- 2024/07/23 11:10 2 年前
CPP
#include<bits/stdc++.h>
using namespace std;
const int MAX=100;
char mp[MAX+5][MAX+5];
int step[MAX+5][MAX+5],n,m;
bool vis[MAX+5][MAX+5];
struct point
{
int x,y;
};
bool bfs(int x,int y)
{
queue<point> q;
if(mp[x][y]!='0'||vis[x][y]) return 0;
q.push({x,y});
vis[x][y]=1;
step[x][y]=0;
while(!q.empty())
{
point t=q.front();
q.pop();
point v1={t.x-1,t.y},v2={t.x+1,t.y},v3={t.x,t.y-1},v4={t.x,t.y+1};
if(v1.x>0&&mp[v1.x][v1.y]!='0'&&!vis[v1.x][v1.y])
{
q.push(v1);
vis[v1.x][v1.y]=1;
}
if(v2.x<=n&&mp[v2.x][v2.y]!='0'&&!vis[v2.x][v2.y])
{
q.push(v2);
vis[v2.x][v2.y]=1;
}
if(v3.y>0&&mp[v3.x][v3.y]!='0'&&!vis[v3.x][v3.y])
{
q.push(v3);
vis[v3.x][v3.y]=1;
}
if(v4.y<=m&&mp[v4.x][v4.y]!='0'&&!vis[v4.x][v4.y])
{
q.push(v4);
vis[v4.x][v4.y]=1;
}
}
return 1;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++) scanf("%c",&mp[i][j]);
scanf("\n");
}
int c=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++) c+=bfs(i,j);
}
printf("%d",c);
return 0;
}
回复
共 1 条回复,欢迎继续交流。
正在加载回复...