PAT 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×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×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:
Sample Output:
26
解题思路:
1、看完题目,感觉人都快奔溃了,题目这啥意思啊,理解不清楚题意,因此看了算法笔记的翻译,大概理解到该题就是在书上例题的基础上,扩充为了3维,求“1”的块。
2、依据例题的思路,自己独立将该题代码写出来了,但以为求的是块的个数,所以算出的结果不是最终答案,题目要求得是所有卒中核心区中1的个数总和,而不是求卒中核心区的个数
注意点:
1、理解不同维数的增量数组的写法,刚开始写这种BFS题目时,对这个的由来很困惑,对PAT的这个增量数组开始也非常疑惑,但其实它理解起来是很简单的,二维时该数组每个index分别表示的是,上、下、左、右。三维时该数组每个index分别表示的是,前、后、上、下、左、右。例如在X[]中的左右上写上1、-1就表示在X轴的左右进行偏移。
二维是:
int X[]={0,0,1,-1}(左右);
int Y[]={1,-1,0,0}(上下);
三维是
int X[] = {0, 0, 0, 0, 1, -1};//左右
int Y[] = {0, 0, 1, -1, 0, 0}; //上下
int Z[] = {1, -1, 0, 0, 0, 0}; //前后
代码:
#include<iostream>
#include<queue>
using namespace std;
const int maxN = 1290, maxM = 130, maxL = 61;
int Marx[maxL][maxN][maxM];//3维矩阵,用来存储0,1矩阵
bool inq[maxL][maxN][maxM]={false};//是否入队
int X[] = {0, 0, 0, 0, 1, -1};//左右
int Y[] = {0, 0, 1, -1, 0, 0}; //上下
int Z[] = {1, -1, 0, 0, 0, 0}; //前后
int m, n, l, t;
int count = 0;//统计1的个数
struct Node
{
int x, y, z;
}node;
bool judge(int x,int y, int z){
if(x<0||x>=m||y<0||y>=n||z<0||z>=l)
return false;//越界
if(inq[z][x][y]||Marx[z][x][y]==0)
return false;//已入队或该点为0
return true;
}
void BFS(int z,int x,int y){
queue<Node> qN;
node.x = x;
node.y = y;
node.z = z;
qN.push(node);
inq[z][x][y] = true;//设置队首元素已入队
while (!qN.empty())
{
Node top = qN.front();//取队首结点
qN.pop();//弹出队首结点
for (int i = 0; i < 6; i++)//访问队首元素
{
int newX = top.x + X[i];
int newY = top.y + Y[i];
int newZ = top.z + Z[i];
if (judge(newX,newY,newZ))
{
count++;//1的数目增加
node.x = newX;
node.y = newY;
node.z = newZ;
qN.push(node);//入队
inq[newZ][newX][newY] = true;//该点已入队
}
}
}
}
int main(){
scanf("%d%d%d%d", &m, &n, &l, &t);
for (int i = 0; i < l; i++)
{
for (int j = 0; j < m; j++)
{
for (int k = 0; k < n; k++)
{
scanf("%d", &Marx[i][j][k]);//z,x,y的顺序
}
}
}
//求解
int ans = 0;
for (int i = 0; i < l; i++)
{
for (int j = 0; j < m; j++)
{
for (int k = 0; k < n; k++)
{
if (inq[i][j][k] == false && Marx[i][j][k]==1)
{
count = 1;
BFS(i, j, k);
if(count>=t){//不少于t个则为一个块
ans+=count;
}
}
}
}
}
cout << ans<<endl;
system("pause");
return 0;
}