1091 Acute Stroke (30分)
作者: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×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
题意:
三维矩阵,M行N列L层,1表示通,0表示阻碍,当上下左右前后相通的块中点的个数大于T,这记录数据量,求全部大于个数T的连通块的点总数。
思路:
三维的bfs深度优先图遍历。
参考代码:
#include <iostream>
#include <queue>
using namespace std;
struct node {
int x, y, z;
};
bool vis[128][1286][60] = {false};
int m, n, l, t, cnt, ans = 0;
int G[128][1286][60] = {0};
int X[6] = {1, -1, 0, 0, 0, 0};
int Y[6] = {0, 0, 1, -1, 0, 0};
int Z[6] = {0, 0, 0, 0, 1, -1};
bool judge(int x, int y, int z) {
if (x < 0 || y < 0 || z < 0)return false;
if (x >= n || y >= m || z >= l)return false;
if (vis[x][y][z] || G[x][y][z] == 0)return false;
return true;
}
void bfs(node a) {
queue<node> q;
q.push(a);
cnt = 1;
vis[a.x][a.y][a.z] = true;
while (!q.empty()) {
node top = q.front();
q.pop();
for (int i = 0; i < 6; i++) {
int tx = top.x + X[i];
int ty = top.y + Y[i];
int tz = top.z + Z[i];
if (judge(tx, ty, tz)) {
vis[tx][ty][tz] = true;
q.push({tx, ty, tz});
cnt++;
}
}
}
}
int main() {
scanf("%d%d%d%d", &m, &n, &l, &t);
for (int k = 0; k < l; k++)
for (int j = 0; j < m; j++)
for (int i = 0; i < n; i++)
scanf("%d", &G[i][j][k]);
for (int k = 0; k < l; k++)
for (int j = 0; j < m; j++)
for (int i = 0; i < n; i++) {
if (!vis[i][j][k] && G[i][j][k] == 1) {
bfs({i, j, k});
if (cnt >= t)ans += cnt;
}
}
printf("%d", ans);
}
如有错误,欢迎指正