1091. Acute Stroke (30)
題目如下:
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這道題的敘述相當的費解,而且圖有些抽象,我讀了兩次都沒有讀懂,后來看了Uncle_Sugar的敘述和解法才明白了題意,其解法簡潔、高效、易懂,下面進行介紹。
題目的本質就是對一個三維數組中1的連通區域中所有1進行計數。因為是三維坐標,因此對1計數不再是簡單的四個方向,而是六個,分別是前后左右上下,也就是圖中給出的六個紅色。個人認為這張圖過于抽象,說白了就是進行三維廣度或者深度搜索,只不過鄰接點的判斷是通過六個方向是否為1來確定的。
Uncle_Sugar的算法巧妙地給出了六個方向的BFS實現,對于某個位置(x,y,z),我們先判斷(x+1,y,z),接著判斷(x-1,y,z),然后是(x,y+1,z)...以此類推,他巧妙的利用三個一維數組實現了用一個循環實現六個方向的遍歷,然后判斷是否是合法范圍,是則計數,最后即可得到1的總數。
下面是Uncle_Sugar的代碼:
# include <cstdio> # include <queue> using std::queue;int map[1286][128][60]; struct loca {int x,y,z;loca(int _x,int _y,int _z):x(_x),y(_y),z(_z){} };int m,n,l,t; int dx[6] = {1,-1,0,0,0,0}; int dy[6] = {0,0,1,-1,0,0}; int dz[6] = {0,0,0,0,1,-1}; int ans = 0; int InRange(int x,int y,int z) {return x < m && x >=0 && y < n&&y >= 0 && z < l && z >= 0; } void bfs(int x,int y,int z) {int ret = 0;queue<loca> que;que.push(loca(x,y,z));map[x][y][z] = 0;ret++;while (!que.empty()){loca tp = que.front();que.pop();x = tp.x;y = tp.y;z = tp.z;for (int i=0;i<6;i++){int nx = x + dx[i];int ny = y + dy[i];int nz = z + dz[i];if (InRange(nx,ny,nz) && map[nx][ny][nz] == 1){map[nx][ny][nz] = 0;ret++;que.push(loca(nx,ny,nz));}}}if (ret>=t)ans += ret; } int main() {scanf("%d%d%d%d",&m,&n,&l,&t);for (int k=0;k<l;k++)for (int i=0;i<m;i++)for (int j=0;j<n;j++)scanf("%d",&map[i][j][k]);for (int k=0;k<l;k++)for (int i=0;i<m;i++)for (int j=0;j<n;j++)if (map[i][j][k]==1)bfs(i,j,k);printf("%d\n",ans);return 0; }
轉載于:https://www.cnblogs.com/aiwz/p/6154038.html
總結
以上是生活随笔為你收集整理的1091. Acute Stroke (30)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android 模拟多线程下载
- 下一篇: import com.sun.image