L3-004 tumor diagnosis (BFS) (30 points)

Keywords: less

L3-004 tumor diagnosis (30 points)

In the diagnosis of tumor diseases, it is very important to calculate tumor volume. Given the suspected tumor area marked in the scanning section of the lesion, please calculate the tumor volume.

Input format:

Input the first line to give four positive integers: m, N, L, T, where m and N are the dimensions of each slice (that is, each slice is a pixel matrix of M × n). The maximum resolution is 1286 × 128; L (≤ 60) is the number of slices; T is an integer threshold (if the volume of the connected body of the suspected tumor is less than T, the small piece is ignored).

Finally, L slices are given. Each is represented by a matrix of M × N consisting of 0 and 1, where 1 represents the suspected tumor pixel and 0 represents the normal pixel. Because the slice thickness can be regarded as a constant, we can get the volume only by counting the number of 1 in the connectome. The trouble is, there may be multiple tumors. At this time, we only count those whose volume is not less than T. Two pixels are considered "connected". If they have a common section, as shown in the figure below, all six red pixels are connected with blue pixels.

Output format:

Outputs the total volume of the tumor in one row.

Input example:

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

Output example:

26

Title Solution

dfs stack overflow

Code

#include <bits/stdc++.h>
using namespace std;
typedef vector<vector<vector<int> > > Img;
struct Node
{
    int r,c,h;
    Node(int _r, int _c, int _h)
    {
        r = _r, c = _c, h = _h;
    }
};
int m, n, l, t;
vector<int> dr{-1,1,0,0,0,0};
vector<int> dc{0,0,-1,1,0,0};
vector<int> dh{0,0,0,0,-1,1};
bool in_Img(int r, int c, int h)
{
    return r >= 0 && r < n && c >= 0 && c < m && h >= 0 && h < l;
}
int bfs(Img &img, Img &flag, int r, int c, int h, int d)
{
    int nr, nc, nh;
    int cnt = 0;
    queue<Node> q;
    q.push(Node(r, c, h));
    flag[r][c][h] = d;
    cnt++;
    while(!q.empty())
    {
        Node u = q.front();q.pop();
        for(int i=0;i<6;i++)
        {
            nr = u.r + dr[i];
            nc = u.c + dc[i];
            nh = u.h + dh[i];
            if(in_Img(nr, nc, nh) && img[nr][nc][nh] == 1 && flag[nr][nc][nh] == 0)
            {
                flag[nr][nc][nh] = d;
                q.push(Node(nr,nc,nh));
                cnt++;
            }
        }
    }
    return cnt;
}
int solve(Img &img, Img &flag)
{
    int sum = 0;
    int d = 0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            for(int k=0;k<l;k++)
            {
                if(flag[i][j][k] == 0 && img[i][j][k] == 1)
                {
                    ++d;
                    int tt = bfs(img, flag, i, j, k, d);
                    if(tt >= t)
                        sum += tt;
                }
            }
        }
    }
    return sum;
}
int main()
{
    cin >> n >> m >> l >> t;
    Img img(n, vector<vector<int> >(m, vector<int>(l, 0)));
    Img flag = img;
    for(int k=0;k<l;k++)
    {
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                cin >> img[i][j][k];
            }
        }
    }
    cout << solve(img, flag) << endl;
    return 0;
}

Posted by StathisG on Mon, 02 Dec 2019 05:46:34 -0800