PAT甲级——1091 Acute Stroke (广度优先搜索BFS)
Posted yinhao-ing
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT甲级——1091 Acute Stroke (广度优先搜索BFS)相关的知识,希望对你有一定的参考价值。
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/94207638
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×N matrix, and the maximum resolution is 1286 by 128); L (≤) 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×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
我一开始没理解题目,看完别人的代码才明白,题目本身不难,比较坑的就是题意理解了~
题目大意:在一堆大脑切片里寻找有问题的块里面的值为1的节点数,一共有L片切片,每个切片都由MxN的像素点矩阵组成,形成一个长M宽N高L的长方体。每个像素点的值为0或1, 1就是有问题的像素点,每个节点的邻接点为上下左右前后这6个点。有问题的块为相互连接的值为1且数目超过T的像素点的集合,将有问题的块里面的值为1的像素点的数目累加即为答案。
思路:遍历三维数组,对每个值为1的像素点进行BFS,并且标记已经访问过的节点。
1 #include <iostream> 2 #include <queue> 3 using namespace std; 4 5 struct node 6 int x, y, z; 7 ; 8 9 int M, N, L, T, 10 pixel[61][1287][129], 11 X[6] = -1,1,0,0,0,0 , 12 Y[6] = 0,0,-1,1,0,0 , 13 Z[6] = 0,0,0,0,-1,1 ; 14 bool visit[61][1287][129] = false ; 15 16 bool check(int& x, int& y, int& z); 17 int count(const int& x, const int& y, const int& z); 18 19 int main() 20 21 int total = 0; 22 scanf("%d%d%d%d", &M, &N, &L, &T); 23 for (int i = 0; i < L; i++) 24 for (int j = 0; j < M; j++) 25 for (int k = 0; k < N; k++) 26 scanf("%d", &pixel[i][j][k]); 27 for (int i = 0; i < L; i++) 28 for (int j = 0; j < M; j++) 29 for (int k = 0; k < N; k++) 30 if (pixel[i][j][k] == 1 && visit[i][j][k] == false) 31 total += count(i, j, k); 32 33 34 35 36 printf("%d\n", total); 37 return 0; 38 39 40 int count(const int& x, const int& y, const int& z) 41 int n = 0; 42 node dot = x,y,z ; 43 queue<node> Q; 44 Q.push(dot); 45 visit[x][y][z] = true; 46 while (!Q.empty()) 47 dot = Q.front(); 48 Q.pop(); 49 n++; 50 for (int i = 0; i < 6; i++) 51 node tmp; 52 tmp.x = dot.x + X[i]; 53 tmp.y = dot.y + Y[i]; 54 tmp.z = dot.z + Z[i]; 55 if (check(tmp.x, tmp.y, tmp.z)) 56 Q.push(tmp); 57 visit[tmp.x][tmp.y][tmp.z] = true; 58 59 60 61 if (n >= T) 62 return n; 63 else 64 return 0; 65 66 67 bool check(int& x, int& y, int& z) 68 if (x < 0 || x >= L || y < 0 || y >= M || z < 0 || z >= N || 69 pixel[x][y][z] == 0 || visit[x][y][z] == true) 70 return false; 71 72 return true; 73
以上是关于PAT甲级——1091 Acute Stroke (广度优先搜索BFS)的主要内容,如果未能解决你的问题,请参考以下文章
PAT甲级——1091 Acute Stroke (广度优先搜索BFS)
PAT 1091 Acute Stroke [难][bfs]