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 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 }

原文地址:https://www.cnblogs.com/yinhao-ing/p/11107933.html

时间: 2024-11-05 18:41:04

PAT甲级——1091 Acute Stroke (广度优先搜索BFS)的相关文章

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

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

算法与数据结构基础 - 广度优先搜索(BFS)

BFS基础 广度优先搜索(Breadth First Search)用于按离始节点距离.由近到远渐次访问图的节点,可视化BFS 通常使用队列(queue)结构模拟BFS过程,关于queue见:算法与数据结构基础 - 队列(Queue) 最直观的BFS应用是图和树的遍历,其中图常用邻接表或矩阵表示,例如 LeetCode题目 690. Employee Importance: // LeetCode 690. Employee Importance/* class Employee { publi

广度优先搜索(BFS)

广度优先 Description: 阿狸被困在迷宫,snoopy要去救他,snoopy可以向上.下.左.右四个方向行走,每走一步(格)就要喝掉一瓶益力多.现在给它一个迷宫地图请问:snoopy最少需要多少瓶益力多才能走出迷宫? Input: 先输入一个数t,表示测试的数据个数, 下面输入的就是t个迷宫, 每个迷宫的输入都应包含以下数据, 输入迷宫的大小 n(n<=15),表示迷宫大小为n*n. 再输入迷宫, 用大写字母“S”表示snoopy的位置, 用小写字母“E”表示阿狸被困的位置, 用“.”

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

【算法导论】--C++实现广度优先搜索bfs

一.题目 根据上次随机生成的100个顶点的无向图和有向图,对其进行广度优先搜索. 二.理解广度优先搜索 广度优先搜索可以将其想象成水滴落入水面溅起了的一圈一圈的涟漪,是由一个起始点开始一圈一圈进行扩散搜索的. [课上老师是这样说的,大家想象一下,发现其实非常形象] 广度优先搜索总是从一个起始点出发,首先扩散这个点周围所有的邻居,然后邻居在去扩散邻居的邻居(*^-^*)...然后一直到最后将整张图都扩散完. 三.代码实现 对于第一次随机生成100个顶点的图进行了细节的修改,将每个顶点的类型改为了自

深度优先搜索DFS和广度优先搜索BFS

DFS简介 深度优先搜索,从起点开始按照某个原则一直往深处走,直到找到解,或者走不下去,走不下去则回溯到前一节点选择另一条路径走,直到找到解为止. BFS简介 广度优先搜索,从起点开始先搜索其相邻的节点,由此向外不断扩散,直到找到解为止. 举例解释 从1开始去寻找5 DFS: 原则:优先选择左手边 过程:1-2-3-4-6-4-5 BFS: 队列情况:1 2.5     5.3 5出来则找到 遍历图中所有点 DFS: 原则:优先选择左手边 过程:1-2-3-4-6-4-5 BFS: 队列情况:1