1091 Acute Stroke(30 分)
作者: CHEN, Yue
单位: 浙江大学
时间限制: 600 ms
内存限制: 64 MB
代码长度限制: 16 KB
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.
【插图】
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
这道题我在做的时候没有完全读懂题目,导致没有认识到这不是一个二维平面的bfs,而是一个三维的bfs。英语还是硬伤啊
回归正题,其实题目读懂了思路不难想出来:就是一个三维的bfs,设置三个数组存储6个方向x, y, z各需要哪些动作,然后记录区域中为1的数目就可以了,最后只有1的数目大于T时才被记入,否则就不计入。
AC代码:
#include <cstdio>
#include <string.h>
#include <queue>
using namespace std;
int brain[65][1290][133];
int m, n, l, t, total = 0;
int posx[] = {1, 0, -1, 0, 0, 0};
int posy[] = {0, 1, 0, -1, 0, 0};
int posz[] = {0, 0, 0, 0, 1, -1};
bool enqueue[65][1290][133] = {0};
struct Node {
int x, y, z;
};
void bfs();
bool judge(int, int, int);
int main() {
scanf("%d%d%d%d", &m, &n, &l, &t);
for(int i = 0; i < l; ++i) {
for(int j = 0; j < m; ++j) {
for(int k = 0; k < n; ++k) {
scanf("%d", &brain[i][j][k]);
}
}
}
bfs();
printf("%d\n", total);
return 0;
}
void bfs() {
for(int i = 0; i < l; ++i) {
for(int j = 0; j < m; ++j) {
for(int k = 0; k < n; ++k) {
if(brain[i][j][k] == 1 && !enqueue[i][j][k]) {
queue<Node> q;
Node n;
n.z = i, n.y = j, n.x = k;
q.push(n);
enqueue[i][j][k] = true;
int rnum = 0;
while(q.size() != 0) {
for(int ii = 0; ii < 6; ++ii) {
int newx = q.front().x + posx[ii];
int newy = q.front().y + posy[ii];
int newz = q.front().z + posz[ii];
if(judge(newx, newy, newz)) {
Node jn;
jn.x = newx, jn.y = newy, jn.z = newz;
enqueue[newz][newy][newx] = true;
q.push(jn);
}
}
q.pop();
++rnum;
}
if(rnum >= t) {
total += rnum;
}
}
}
}
}
}
bool judge(int x, int y, int z) {
if(x < 0 || y < 0 || x >= n || y >= m || z < 0 || z >= l) {
return false;
}
if(brain[z][y][x] == 0 || enqueue[z][y][x]) {
return false;
}
return true;
}
如有错误,欢迎指摘。