1. 程式人生 > >PAT1091:Acute Stroke

PAT1091:Acute Stroke

坐標 個數 float names 作者 pat tle ica cat

1091. Acute Stroke (30)

時間限制 400 ms 內存限制 65536 kB 代碼長度限制 16000 B 判題程序 Standard 作者 CHEN, Yue

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 by 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 by 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.

技術分享圖片
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

思路

由題意,M行N列的矩陣表示一片腦部切圖,矩陣中的值——0表示該位置一切正常,1表示該位置為腫瘤區,L表示腦部切圖的片數,由此構建了一個M、N、L的三維的矩陣。
現在讓你確定統計腫瘤區的個數,註意只有上下前後左右相鄰的腫瘤區且這些相鄰的區域個數必須不小於T才能被統計。
歸根結底其實就是一個3D圖的連通分量問題,即統計每一個節點數不小於T的連通分量的節點數之和。
用BFS或者DFS都行,這裏用BFS更好,DFS遞歸可能會棧溢出。

代碼
#include<iostream>
#include<queue>
using namespace std;
/*
坐標系如下
xxxxxxxxxxxxxxxxxx + →
z
z
z
z
z
z
z
+
↓
y軸垂直屏幕射出

*/
class node
{
public:
    int x,z,y;
    node(int a,int b, int c){x = a;z = b;y = c;}
};
//6個方向分別為上下前後左右
int X[6] = {0,0,0,0,-1,1};
int Y[6] = {1,-1,0,0,0,0};
int Z[6] = {0,0,1,-1,0,0};
int graph[1290][130][65];
bool visit[1290][130][65];
int M,N,L,T; // z x y T


bool check(int x,int z,int y)
{
    if(x < 0 || z < 0 || y < 0 || x >= N || z >= M || y >= L)
        return false;
    if(graph[x][z][y] == 0 || visit[x][z][y])
        return false;
    return true;
}

int bfs(int x,int z,int y)
{
      int cnt = 0;
      node tmp(x,z,y);
      queue<node> q;
      q.push(tmp);
      visit[x][z][y] = true;
      while(!q.empty())
      {
          node f = q.front();
          q.pop();
          cnt++;
          for(int i = 0;i < 6;i++)
          {
              int newx = f.x + X[i];
              int newz = f.z + Z[i];
              int newy = f.y + Y[i];
              if(check(newx,newz,newy))
              {
                  visit[newx][newz][newy] = true;
                  tmp.x = newx;
                  tmp.y = newy;
                  tmp.z = newz;
                  q.push(tmp);
              }
          }
      }
      if(cnt >= T)
        return cnt;
      else
        return 0;
}

int main()
{
    cin >> M >> N >> L >> T;
    for(int y = 0;y < L;y++)
    {
        for(int z = 0; z < M;z++)
        {
            for(int x = 0;x < N;x++)
            {
                cin >> graph[x][z][y];
                visit[x][z][y] = false;
            }
        }
    }
    int res = 0;
    for(int y = 0;y < L;y++)
    {
        for(int z = 0; z < M;z++)
        {
            for(int x = 0;x < N;x++)
            {
                if(graph[x][z][y] == 1 && !visit[x][z][y])
                    res += bfs(x,z,y);
            }
        }
    }

    cout << res << endl;

}

  

PAT1091:Acute Stroke