Skip to content

1091 Acute Stroke

Statement

Metadata

  • 作者: CHEN, Yue
  • 单位: 浙江大学
  • 代码长度限制: 16 KB
  • 时间限制: 600 ms
  • 内存限制: 64 MB

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 \times N matrix, and the maximum resolution is 1286 by 128); L (\le 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 \times 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

Solution

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6;
int n, m, L, T, now, tot;
int G[N], vis[N];
int Move[][3] = {
        {0, 0, 1},
        {0, 0, -1},
        {0, 1, 0},
        {0, -1, 0},
        {1, 0, 0},
        {-1, 0, 0},
};

inline int id(int x, int y, int z) {
    ++x, ++y, ++z;
    return (x - 1) * (n * m) + (y - 1) * m + z;
}

bool ok(int x, int y, int z) {
    if (x < 0 || x >= L || y < 0 || y >= n || z < 0 || z >= m || vis[id(x, y, z)] || G[id(x, y, z)] == 0)
        return false;
    return true;
}

struct E {
    int x, y, z;
    E() {}
    E(int x, int y, int z) : x(x), y(y), z(z) {}
};

void bfs(int x, int y, int z) {
    queue<E> que;
    que.emplace(x, y, z);
    while (!que.empty()) {
        x = que.front().x;
        y = que.front().y;
        z = que.front().z;
        que.pop();
        vis[id(x, y, z)] = 1;
        ++now;
        for (int i = 0; i < 6; ++i) {
            int nx = x + Move[i][0];
            int ny = y + Move[i][1];
            int nz = z + Move[i][2];
            if (ok(nx, ny, nz)) {
                vis[id(nx, ny, nz)] = 1;
                que.emplace(nx, ny, nz);
            }
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m >> L >> T;
    memset(G, 0, sizeof G);
    memset(vis, 0, sizeof vis);
    tot = 0;
    for (int i = 0; i < L; ++i) {
        for (int j = 0; j < n; ++j) {
            for (int k = 0; k < m; ++k) {
                cin >> G[id(i, j, k)];
            }
        }
    }
    for (int i = 0; i < L; ++i) {
        for (int j = 0; j < n; ++j) {
            for (int k = 0; k < m; ++k) {
                if (G[id(i, j, k)] == 1 && vis[id(i, j, k)] == 0) {
                    now = 0;
                    bfs(i, j, k);
                    if (now >= T) {
                        tot += now;
                    }
                }
            }
        }
    }
    cout << tot << "\n";
    return 0;
}

Last update: May 4, 2022
Back to top