#include<iostream>
#include<cstdlib>
using namespace std;
#define max 50000
struct pos {
int x;
int y;
};
typedef struct se
{
pos data[max];
int front, rear;
} Sequeue;
void init(Sequeue*& Q)
{
Q = (Sequeue*)malloc(sizeof(Sequeue));
Q->front = -1;
Q->rear = -1;
}
int empty(Sequeue* Q)
{
if (Q->rear == Q->front)
return 1;
else
return 0;
}
void insertsequeue(Sequeue*& Q, int i, int j)
{
if ((Q->rear + 1) % max == Q->front)
{
cout << "队列已满!" << endl;
exit(0);
}
Q->rear = (Q->rear + 1) % max;
Q->data[Q->rear].x = i;
Q->data[Q->rear].y = j;
}
void delsequeue(Sequeue*& Q, int& i, int& j)
{
if (Q->rear == Q->front)
{
cout << "队列已空!" << endl;
exit(0);
}
Q->front = (Q->front + 1) % max;
i = Q->data[Q->front].x;
j = Q->data[Q->front].y;
}
int count(int a[50][50], int m, int n)
{
int x = 0;
int i, j, pi, pj;
Sequeue* q;
init(q);
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
if (a[i][j])
{
insertsequeue(q, i, j);
while (!empty(q))
{
delsequeue(q, pi, pj);
a[pi][pj] = 0;
if(pi-1>=0&&a[pi-1][pj])insertsequeue(q, pi-1, pj);
if(pi+1<=m-1&&a[pi+1][pj])insertsequeue(q, pi+1, pj);
if(pj+1<=n-1&&a[pi][pj+1])insertsequeue(q, pi, pj+1);
if(pj-1>=0&&a[pi][pj-1])insertsequeue(q, pi, pj-1);
}
x++;
}
}
}
return x; //x为计算出在细胞在个数
}
int main()
{
int test[50][50];
int i, j, m, n;
cin >> m;
cin >> n;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
cin >> test[i][j];
cout << count(test, m, n);
}
标签:964,pi,int,细胞,pj,Sequeue,front,SWUST,rear
From: https://blog.csdn.net/2301_79744389/article/details/137569068