题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=27

#include <stdio.h>
#include <string.h>

int t;
int r,c;
int maps[105][105];
int color[105][105];
int to[4][2]= {{0,1},{0,-1},{1,0},{-1,0}};


void dfs(int i,int j)
{
    if(maps[i][j]==1&&color[i][j]==0)
    {
        color[i][j]=1;
        for(int k=0; k<4; k++)
        {
            int tx=i+to[k][0];
            int ty=j+to[k][1];
            if(tx>-1&&tx<r&&ty>-1&&ty<c)
            {
                dfs(tx,ty);
            }
        }
    }
}


int main()
{
    scanf("%d",&t);
    while(t--)
    {
        memset(maps,0,sizeof(maps));
        memset(color,0,sizeof(color));
        scanf("%d%d",&r,&c);
        for(int i=0; i<r; i++)
        {
            for(int j=0; j<c; j++)
            {
                scanf("%d",&maps[i][j]);
            }
        }
        int Count=0;
        for(int i=0; i<r; i++)
        {
            for(int j=0; j<c; j++)
            {
                if(color[i][j]==0&&maps[i][j]==1)
                {
                    Count++;
                    dfs(i,j);
                }
            }
        }
        printf("%d\n",Count);
    }
    return 0;
}

 

相关文章:

  • 2021-08-04
  • 2022-01-31
  • 2021-09-01
  • 2021-09-10
  • 2021-05-21
  • 2022-12-23
  • 2021-09-29
  • 2021-06-14
猜你喜欢
  • 2021-07-10
  • 2022-01-15
  • 2022-12-23
  • 2021-09-15
  • 2021-10-10
  • 2021-05-19
  • 2022-12-23
相关资源
相似解决方案