1. 程式人生 > 其它 >1091 Acute Stroke (30 分)

1091 Acute Stroke (30 分)

技術標籤:PAT甲級

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

題目大意:給定一個三維陣列,陣列元素為1時代表有腫塊,陣列元素為0時表示沒有腫塊,腫塊的大小大於等於給定的數值時才能加入總體積
分析:設定X/Y/Z陣列判斷方向,每次遍歷這三個陣列時相當於遍歷與一個畫素點相鄰的六個畫素點。定義panduan函式判斷該畫素點是否可以加入。設定visit標記結點有沒有被訪問,每個結點只能被訪問一次。用廣度優先搜尋的模板實現

AC程式碼

#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;
}