30分bfs,求大佬帮看看
查看原帖
30分bfs,求大佬帮看看
843406
sjm2022楼主2024/9/5 21:19

测试点信息

#include <bits/stdc++.h>
using namespace std;
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
int lie=0;
int ans=0;
int hang=0;
int tu[100][100]={0};
struct ai
{
    int x;
    int y;
};
void bfs(int a,int b)
{
    queue <ai> q;
    ans++;
    tu[a][b]=0;
    q.push({a,b});
    do
    {
        ai t=q.front();
        q.pop();
        for(int i=0;i<=3;i++)
        {
            int ix=t.x+dx[i];
            int iy=t.y+dy[i];
            if((ix>=0)&&(ix<hang)&&(iy>=0)&&(iy<lie)&&(tu[ix][iy]))
            {
                q.push({ix,iy});
                tu[ix][iy]=0;
            }
        }
    }
    while(!q.empty());
}
int main()
{
    cin>>hang>>lie;
    fill(tu[0],tu[102],0);
    for(int i=0;i<hang;i++)
    {
        for(int j=0;j<lie;j++)
        {
            char temp=getchar();
            int t=temp-48;
            if(t!=0)
            {
                tu[i][j]=1;
            }
        }
    }
    
    for(int i=0;i<hang;i++)
    {
        for(int j=0;j<lie;j++)
        {
            if(tu[i][j])
            {
                bfs(i,j);
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}
2024/9/5 21:19
加载中...