One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given the results of image analysis in which the core regions are identified in each MRI slice, your job is to calculate the volume of the stroke core.
Input Specification:
Each input file contains one test case. For each case, the first line contains 4 positive integers: M, N, L and T, where M and N are the sizes of each slice (i.e. pixels of a slice are in an M×N matrix, and the maximum resolution is 1286 by 128); L (≤60) is the number of slices of a brain; and T is the integer threshold (i.e. if the volume of a connected core is less than T, then that core must not be counted).
Then L slices are given. Each slice is represented by an M×N matrix of 0's and 1's, where 1 represents a pixel of stroke, and 0 means normal. Since the thickness of a slice is a constant, we only have to count the number of 1's to obtain the volume. However, there might be several separated core regions in a brain, and only those with their volumes no less than Tare counted. Two pixels are connected and hence belong to the same region if they share a common side, as shown by Figure 1 where all the 6 red pixels are connected to the blue one.
【插图】
Figure 1
Output Specification:
For each case, output in a line the total volume of the stroke core.
Sample Input:
3 4 5 2
1 1 1 1
1 1 1 1
1 1 1 1
0 0 1 1
0 0 1 1
0 0 1 1
1 0 1 1
0 1 0 0
0 0 0 0
1 0 1 1
0 0 0 0
0 0 0 0
0 0 0 1
0 0 0 1
1 0 0 0
Sample Output:
26
------------------------------------这是题目和解题的分割线------------------------------------
【BFS】矩阵“块”问题 | 迷宫的最少步数 | 队列副本 这个套路。
我真的看不懂题目哇,这是怎么看出来三维数组的,代码算出来是26,我却怎么也数不出来QAQ 看中文解释也看不懂,我没救了QAQ 但是题目确实挺简单的(捶桌
#include<cstdio>
#include<queue>
using namespace std;
struct node
{
int x,y,z;
}tmp;
int a[1300][200][100],flag[1300][200][100] = {0},M,N,L,T;
//上下左右前后
int X[6] = {0,0,1,-1,0,0};
int Y[6] = {1,-1,0,0,0,0};
int Z[6] = {0,0,0,0,1,-1};
int judge(int x,int y,int z)
{
//超出边界
if(x<0||x>=N||y<0||y>=M||z<0||z>=L) return 0;
//进入过队列or数值不为1
if(flag[x][y][z]==1||a[x][y][z]==0) return 0;
return 1;
}
int bfs(int x,int y,int z)
{
queue<node> q;
tmp.x = x;
tmp.y = y;
tmp.z = z;
q.push(tmp); //入队
flag[x][y][z] = 1; //标记
int num = 0; //计数
while(!q.empty())
{
node top = q.front(); //取出队首元素
q.pop(); //出队列
num++; //该块的"1"元素+1
for(int i=0;i<6;i++)
{
int newX = top.x + X[i];
int newY = top.y + Y[i]; //新坐标
int newZ = top.z + Z[i];
if(judge(newX,newY,newZ)) //判断是否需要访问
{
tmp.x = newX;
tmp.y = newY;
tmp.z = newZ;
q.push(tmp); //进队
flag[newX][newY][newZ] = 1; //标记
}
}
}
if(num>=T) return num; //如果满足条件return
return 0; //否则该块不算数 return 0
}
int main()
{
int i,j,k,count = 0;
scanf("%d%d%d%d",&N,&M,&L,&T);
//输入要小心,最外层是slice
for(i=0;i<L;i++)
for(j=0;j<N;j++)
for(k=0;k<M;k++)
scanf("%d",&a[j][k][i]); //但是这里的坐标要遵从行列slice
for(i=0;i<L;i++)
{
for(j=0;j<N;j++)
for(k=0;k<M;k++)
{
if(a[j][k][i]==1&&flag[j][k][i]==0)
count += bfs(j,k,i); //叠加
}
}
printf("%d\n",count);
return 0;
}