题号: 10258
时限:1000ms
限制内存:32768KB
题目: 细胞分裂
描述
一矩形阵列由数字0到9组成,数字1到9代表细胞,细胞的定义为沿细胞数字上下左右还是细胞数字则为同一细胞,求给定矩形阵列的细胞个数。如:
阵列
4 10
0234500067
1034560500
2045600671
0000000089
有4个细胞。
输入格式
矩阵长宽N,M,均小于100。
读入矩阵。
输出格式
细胞个数ans。
输入样例
4 10
0234500067
1034560500
2045600671
0000000089
输出样例
4
#include<stdio.h>
int map[102][102];
void search(int i,int j)
{
if(map[i][j-1]){
map[i][j-1]=0;
search(i,j-1);
}
if(map[i][j+1]){
map[i][j+1]=0;
search(i,j+1);
}
if(map[i-1][j]){
map[i-1][j]=0;
search(i-1,j);
}
if(map[i+1][j]){
map[i+1][j]=0;
search(i+1,j);
}
}
int main()
{
int n,m,count=0;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i)
{
for(int j=1;j<=m;++j)
scanf("%1d",&map[i][j]);
}
for(int i=1;i<=n;++i)
{
for(int j=1;j<=m;++j)
if(map[i][j]){
count++;
map[i][j]=0;
search(i,j);
}
}
printf("%d\n",count);
return 0;
}