PAT甲级1091

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

/*
#include<cstdio>
const int maxM = 1286, maxN = 128, maxL = 60;
bool inq[maxM][maxN][maxL] = { false };
int brain[maxM][maxN][maxL];
int m, n, l,t;
int countv = 0,volume=0;
bool valid(int x,int y,int z)
{
if (x < 0 || y < 0 || z < 0 || x >= m || y >= n || z >= l)
return false;
if (!brain[x][y][z] || inq[x][y][z])return false;
return true;
}//判断该点是否有效或未被访问
void DFS(int x, int y, int z)
{
if (valid(x, y, z))
{
inq[x][y][z] = true;
countv++;
}
else
return;
DFS(x, y, z + 1);
DFS(x, y, z - 1);
DFS(x, y-1, z);
DFS(x, y+1, z);
DFS(x-1, y, z);
DFS(x+1, y, z);
}//深度优先遍历
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", &brain[j][k][i]);
}
}
}
for (int i = 0; i < l; i++)
{
for (int j = 0; j < m; j++)
{
for (int k = 0; k < n; k++)
{
countv = 0;
if (brain[j][k][i]==1&&inq[j][k][i]==false)
{
DFS(j, k, i);
if (countv >= t)//超过其阈值就加到容量中去
volume += countv;
}
}
}
}
printf("%d", volume);
return 0;
}
递归版DFS解法会导致最后两个点过不了*/
#include<cstdio>
#include<queue>
using namespace std;
struct node
{
	int x, y, z;//位置(x,y,z)
}Node;
int n, m, slice, T;//矩阵为n*m,共有slice层,T为卒中核心区中1的个数的下限
int pixel[1290][130][61];//三维01矩阵
bool inq[1290][130][61] = { false };//记录位置(x,y,z)是否已入过队
int X[6] = { 0,0,0,0,1,-1 };//增量矩阵
int Y[6] = { 0,0,1,-1,0,0 };
int Z[6] = { 1,-1,0,0,0,0 };

bool judge(int x, int y, int z)//判断坐标(x,y,z)是否需要访问
{
	//越界返回false
	if (x >= n || x < 0 || y >= m || y < 0 || z >= slice || z < 0)return false;
	//当前位置为0或(x,y,z)已入过队,则返回false
	if (pixel[x][y][z] == 0 || inq[x][y][z] == true) return false;
	//以上都不满足,返回true
	return true;
}
//BFS函数访问位置(x,y,z)所在的块,将该块中所有的“1”的ing都设置为true
int BFS(int x, int y, int z)
{
	int tot = 0;//计数当前块中1的个数
	queue<node> Q;//定义队列
	Node.x = x, Node.y = y, Node.z = z;//结点Node的位置为(x,y,z)
	Q.push(Node);//将结点Node入队
	inq[x][y][z] = true;//设置位置(x,y,z)已入过队
	while (!Q.empty())
	{
		node top = Q.front();//取出队首元素
		Q.pop();//队首元素出队
		tot++;//当前块中1的个数加1
		for (int i = 0; i < 6; i++)//循环6次,得到6个增量方向
		{
			int newX = top.x + X[i];
			int newY = top.y + Y[i];
			int newZ = top.z + Z[i];
			if (judge(newX, newY, newZ))//新位置(newX,newY,newZ)需要访问
			{//设置Node的坐标
				Node.x = newX, Node.y = newY, Node.z = newZ;
				Q.push(Node);//将结点Node入队
				inq[newX][newY][newZ] = true;//设置(newX,newY,newZ)已入过队
			}
		}
	}
	if (tot >= T)return tot;//如果超过阈值,则返回
	else return 0;//否则不记录该块1的个数
}
int main()
{
	scanf("%d%d%d%d", &n, &m, &slice, &T);
	for (int z = 0; z < slice; z++)
	{
		for (int x = 0; x < n; x++)
		{
			for (int y = 0; y < m; y++)
			{
				scanf("%d", &pixel[x][y][z]);
			}
		}
	}
	int ans = 0;//记录卒中核心区中1的个数总和
	for (int z = 0; z < slice; z++)
	{
		for (int x = 0; x < n; x++)
		{
			for (int y = 0; y < m; y++)
			{
				//如果当前位置为1,且未被访问,则BFS当前块
				if (pixel[x][y][z] == 1 && inq[x][y][z] == false)
				{
					ans += BFS(x, y, z);
				}
			}
		}
	}
	printf("%d\n", ans);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值