# PAT Advanced 1091 Acute Stroke: 3D DFS or BFS

## Overview

PAT Advanced 1091 Acute Stroke Solution: It is actually quite straightforward to find all the connected components by DFS or BFS in the cubic (3D).

## PAT Advanced 1091 Acute Stroke

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 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 Analysis: 3D DFS or BFS

This problem cannot be counted as a difficult one from my own angle, we just need to find all the connected component in the cubic and calculate the volume and test whether the current volume is above (>=) the threshold, if so, we add it into the whole sum which is used to finally determine whether it is acute stroke. So essentially, it becomes a 3D version of doing DFS or BFS. The following is source code of BFS is accepted by PAT OJ to pass this 1091 Acute Stroke problem:

#include <cstdio> #include <vector> #include <queue> #include <algorithm> using namespace std; int vol = 0; int localVolumn = 0; int m, n, l, t; vector< vector< vector<char> > > g; vector< vector< vector<bool> > > v; bool cond(int i, int j, int k) { if (i < 0 || i >= l || j < 0 || j >= m || k < 0 || k >= n) return false; if ('0' == g[i][j][k]) return false; if (v[i][j][k]) return false; return true; } struct Node { int i; int j; int k; Node(int ii=-1, int jj=-1, int kk=-1) : i(ii), j(jj), k (kk) {} }; void bfs(int ii, int jj, int kk) { Node root(ii, jj, kk); queue<Node> q; q.push(root); v[ii][jj][kk] = true; Node tmp; Node t; while (!q.empty()) { t = q.front(); q.pop(); int i = t.i, j = t.j, k = t.k; (localVolumn)++; if (cond(i-1, j, k)) {v[i-1][j][k] = true; tmp.i = i - 1; tmp.j = j; tmp.k = k; q.push(tmp);} if (cond(i+1, j, k)) {v[i+1][j][k] = true; tmp.i = i + 1; tmp.j = j; tmp.k = k; q.push(tmp);} if (cond(i, j-1, k)) {v[i][j-1][k] = true; tmp.i = i; tmp.j = j - 1; tmp.k = k; q.push(tmp);} if (cond(i, j+1, k)) {v[i][j+1][k] = true; tmp.i = i; tmp.j = j + 1; tmp.k = k; q.push(tmp);} if (cond(i, j, k-1)) {v[i][j][k-1] = true; tmp.i = i; tmp.j = j; tmp.k = k - 1; q.push(tmp);} if (cond(i, j, k+1)) {v[i][j][k+1] = true; tmp.i = i; tmp.j = j; tmp.k = k + 1; q.push(tmp);} } } int main() { scanf("%d %d %d %d", &m, &n, &l, &t); vol = 0; g.resize(l); v.resize(l); for (int i = 0; i < l; ++i) { g[i].resize(m); v[i].resize(m); for (int j = 0; j < m; ++j) { g[i][j].resize(n); v[i][j].resize(n); std::fill(v[i][j].begin(), v[i][j].end(), false); } } int tmp; for (int i = 0; i < l; ++i) for (int j = 0; j < m; ++j) for (int k = 0; k < n; ++k) { scanf("%d", &tmp); //scanf("%c", g[i][j][k]); g[i][j][k] = '0' + tmp; } for (int i = 0; i < l; ++i) for (int j = 0; j < m; ++j) for (int k = 0; k < n; ++k) if (!v[i][j][k] && g[i][j][k] == '1') { localVolumn = 0; //dfs(i, j, k); bfs(i, j, k); if (localVolumn >= t) vol += localVolumn; } printf("%d\n", vol); return 0; }

And note if you do it in recursive calls (recursive DFS), you might get segment fault (段错误) for the last two test cases. This is because the stack allocated by PAT OJ might not be big enough. So an iterative version of DFS might be more preferable.

## Summary

PAT Advanced 1091 Acute Stroke Solution: It is actually quite straightforward to find all the connected components by DFS or BFS in the cubic (3D).