PAT 1091. Acute Stroke (30)

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

此题的题目理解很是关键,关键是关于题目中的connnected的理解。题目中的每一个M*N的矩阵可以看做一层,然后总共有L层。我们可以将每一个1/0看做一个点,与它相连的就是他的上下左右前后六个方向的点,例如第一个点的坐标为(0,0,0)那么与它相连的有(1,0,0) (0,1,0) (0,0,1)其他的三个方向不存在。那么我们可以将其看做一个三维的图,两个相邻的都为1的就是存在一条边,然后采用DFS算出每一个连通分量中1的个数就是volume,然后就可以得到答案了
 1 #include <cstdio>
 2 #include <iostream>
 3 #include <queue>
 4
 5 using namespace std;
 6
 7 int m, n, l;
 8 int point[1286][128][60];
 9
10 struct pixel
11 {
12     int x;
13     int y;
14     int z;
15     pixel(int xx, int yy, int zz){ x = xx; y = yy; z = zz; }
16 };
17
18 int DFS(int x, int y, int z)
19 {
20     int volume = 0;
21     queue<pixel> que;
22     pixel p(x, y, z);
23     point[x][y][z] = 0;
24     que.push(p);
25     while (!que.empty())
26     {
27         pixel p = que.front();
28         volume++;
29         que.pop();
30         if (p.x - 1 >= 0 && point[p.x - 1][p.y][p.z])
31         {
32             que.push(pixel(p.x - 1, p.y, p.z));
33             point[p.x - 1][p.y][p.z] = 0;
34         }
35         if (p.x + 1 < m && point[p.x + 1][p.y][p.z])
36         {
37             que.push(pixel(p.x + 1, p.y, p.z));
38             point[p.x + 1][p.y][p.z] = 0;
39         }
40         if (p.y - 1 >= 0 && point[p.x][p.y - 1][p.z])
41         {
42             que.push(pixel(p.x, p.y - 1, p.z));
43             point[p.x][p.y - 1][p.z] = 0;
44         }
45         if (p.y + 1 < n && point[p.x][p.y + 1][p.z])
46         {
47             que.push(pixel(p.x, p.y + 1, p.z));
48             point[p.x][p.y + 1][p.z] = 0;
49         }
50         if (p.z - 1 >= 0 && point[p.x][p.y][p.z - 1])
51         {
52             que.push(pixel(p.x, p.y, p.z - 1));
53             point[p.x][p.y][p.z - 1] = 0;
54         }
55         if (p.z + 1 < l && point[p.x][p.y][p.z + 1])
56         {
57             que.push(pixel(p.x, p.y, p.z + 1));
58             point[p.x][p.y][p.z + 1] = 0;
59         }
60     }
61     return volume;
62 }
63
64 int main()
65 {
66     int threshold;
67     scanf("%d%d%d%d", &m, &n, &l, &threshold);
68     for (int k = 0; k < l; k++)
69         for (int i = 0; i < m; i++)
70             for (int j = 0; j < n; j++)
71                 scanf("%d", &point[i][j][k]);
72     int volume = 0;
73     for (int k = 0; k < l; k++)
74     {
75         for (int i = 0; i < m; i++)
76         {
77             for (int j = 0; j < n; j++)
78             {
79                 if (point[i][j][k])
80                 {
81                     int vol = DFS(i, j, k);
82                     if (vol >= threshold)
83                         volume += vol;
84                 }
85             }
86         }
87     }
88     printf("%d", volume);
89 }
时间: 2024-10-17 07:07:34

PAT 1091. Acute Stroke (30)的相关文章

1091. Acute Stroke (30)【搜索】——PAT (Advanced Level) Practise

题目信息 1091. Acute Stroke (30) 时间限制400 ms 内存限制65536 kB 代码长度限制16000 B 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,

【PAT甲级】1091 Acute Stroke (30分)

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

PAT 1091 Acute Stroke [难][bfs]

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 th

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 Spec

PAT甲题题解-1091. Acute Stroke (30)-BFS

题意:给定三维数组,0表示正常,1表示有肿瘤块,肿瘤块的区域>=t才算是肿瘤,求所有肿瘤块的体积和 这道题一开始就想到了dfs或者bfs,但当时看数据量挺大的,以为会导致栈溢出,所以并没有立刻写,想有没有别的办法.然而结果是,实在想不出别的办法了,所以还是尝试写写dfs.bfs. 一开始先用了dfs,最后两个样例段错误,估计是栈溢出了.之所以dfs栈溢出,因为dfs的时候每个状态都会存储在堆栈里,就好比dfs的第一个状态,一直保存到最后整个dfs结束.而bfs是存储在队列中,每次队列都会有状态取

PAT (Advanced Level) 1091. Acute Stroke (30)

BFS求连通块.递归会爆栈. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<map> #include<queue> #include<stack> #include<vector> using namespace std; int M,N,L,T; int A[70][1300][133]; bo

PAT甲级——1091 Acute Stroke (广度优先搜索BFS)

本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/94207638 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

pat1091. Acute Stroke (30)

1091. Acute Stroke (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 iden

PAT 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. 输入描述: