1091 Acute Stroke (30 分)

1091 Acute Stroke (30 分)
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 T are 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.

figstroke.jpg
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

The main idea of ​​the topic: Given a three-dimensional array, when the array element is 1, it means there is a lumps, and when the array element is 0, it means there are no lumps. The total volume
analysis can only be added when the size of the lumps is greater than or equal to the given value : set X/Y/Z array To determine the direction, each traversal of these three arrays is equivalent to traversing six pixels adjacent to one pixel. Define panduan function to judge whether the pixel can be added. Set visit to mark whether the node has been visited, each node can only be visited once. Implemented with breadth-first search template

AC code

#include<bits/stdc++.h>
using namespace std;
int X[6] = {
    
    1, 0, 0, -1, 0, 0};
int Y[6] = {
    
    0, 1, 0, 0, -1, 0};
int Z[6] = {
    
    0, 0, 1, 0, 0, -1};//记住三维的构造方式
int region[1300][130][70];
bool visit[1300][130][70];
int M, N, L, T,sum=0;
struct node{
    
    
  int x, y, z;
};
int panduan(int x,int y,int z){
    
    
  if(x<=0||x>N||y<=0||y>M||z<=0||z>L)
    return false;
  if(visit[x][y][z]==true||region[x][y][z]==0)
    return false;

  return true;
}
/*熟记广度优先搜索的模板
1.任选图中一个结点访问,入队,并将这个顶点标记为已访问
2.当队列不空时循环执行:出队,依次检查出队顶点的所有邻接顶点,访问没有被访问过的邻接顶点并将其入队
3.当队列为空时跳出循环,广度优先搜索完成
*/
int BFS(node topNode){
    
    
  int cnt = 0;
  queue<node> q;
  visit[topNode.x][topNode.y][topNode.z] = true;//将入队结点标记为已访问
  q.push(topNode);
  int x, y, z;
  while(!q.empty()){
    
    
    node temp = q.front();
    q.pop();
    cnt++;//每一个结点都需要被抛出,记录抛出结点的个数
    for (int i = 0; i < 6;i++){
    
    
      x=temp.x + X[i];
      y=temp.y + Y[i];
      z=temp.z + Z[i];
      if(panduan(x,y,z)){
    
    
        topNode = {
    
    x, y, z};
        q.push(topNode);
        visit[x][y][z] = true;
      }//检查出队顶点的所有邻接顶点
    }
  }
  if(cnt>=T)
  return cnt;
  else
    return 0;
}

int main(){
    
    
  cin >> M >> N >> L >> T;
  for (int i = 1; i <= L;i++){
    
    
    for (int j = 1; j <= M;j++){
    
    
      for (int k = 1; k <= N;k++)
        cin >> region[k][j][i];
    }
  }
  for (int i = 1; i <= L;i++){
    
    
    for (int j = 1; j <= M;j++){
    
    
      for (int k = 1; k <= N;k++){
    
    
        if(visit[k][j][i]==false&&region[k][j][i]==1){
    
    
        node temp = {
    
    k, j, i};
        sum += BFS(temp);
        }
      } 
    }
  }
  printf("%d", sum);

  return 0;
}

Guess you like

Origin blog.csdn.net/weixin_48954087/article/details/113796730