1091. Acute Stroke (30)

来源:互联网 发布:腾达ac6 访客网络限速 编辑:程序博客网 时间:2024/06/10 17:43

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 21 1 1 11 1 1 11 1 1 10 0 1 10 0 1 10 0 1 11 0 1 10 1 0 00 0 0 01 0 1 10 0 0 00 0 0 00 0 0 10 0 0 11 0 0 0
Sample Output:
26

这道题目的意思就是查找三维上的相邻点连接总数量,相当于三维空间寻找集合,

首先想到的就是DFS不过写完发现有2个段错误,查了很久才知道是递归栈太深导致的段错误,

因为递归是隐式用栈,于是显式用栈来写DFS解决了段错误,用队列的BFS也应该可以;

#include <iostream>#include <stack>using namespace std;struct Node{    int x, y, z;    Node(int a, int b, int c):x(a), y(b), z(c){};};int brain[60][1286][128] = {0};int step[6][3]={    1, 0, 0,    -1, 0, 0,    0, 1, 0,    0, -1, 0,    0, 0, 1,    0, 0, -1};int M, N, L, T;int total = 0, counter = 0;void init(){    int temp;    cin >> 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++)            {                cin >> temp;                brain[i][j][k] = temp;            }        }    }}void DFS(int x, int y, int z){    stack<Node> mystack;    int l, m, n;    Node first(x, y, z);    mystack.push(first);    while(!mystack.empty())    {        Node temp = mystack.top();        mystack.pop();        for(int i=0; i<6; i++)        {            l = step[i][0]+temp.x;            m = step[i][1]+temp.y;            n = step[i][2]+temp.z;            if((l>=0 && l<L) && (m>=0 && m<M) && (n>=0 && n<N) && brain[l][m][n]==1)            {                brain[l][m][n] = 0;                counter++;                Node temp(l, m, n);                mystack.push(temp);            }        }    }}int main(){    init();    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)                {                    brain[i][j][k] = 0;                    counter = 1;                    DFS(i, j, k);                    if(counter >= T)                        total += counter;                }            }        }    }    cout << total << endl;    return 0;}


0 0
原创粉丝点击