PAT 甲级 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.

输入

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.

输出

For each case, output in a line the total volume of the stroke core.

思路

这道题题目看了好久都没看懂,在二刷的时候总算明白了,大概就是说给你L个切片,每个切片都一样大,然后每个切片可以堆在一起,其中1的地方代表有问题。所以如果不限制大小,这道题答案就是1的个数。然后现在限制大小,就是说这些有问题的地方只有大于T才计入,然后看题目说他们必须连续的才会被算作一起,也就是说在空间上上下左右前后6个方向上看是否有1有就为连续。最后发现也就是一道三维的空间bfs。
三维bfs就是设置一个方向,每次只能从一个方向出发,不能对角线走,所以dx dy dz在第i的值中只有一个等于1或-1其他都为0,然后判断是否是走出了大小(边界)即可。

代码

#include<iostream>
#include<cstdio>
#include<stdlib.h>
#include<string>
#include<vector>
#include<queue>
using namespace std;
int graph[65][1300][150];
int dx[] = {
    
     1,-1,0,0,0,0 };
int dy[] = {
    
     0,0,1,-1,0,0 };
int dz[] = {
    
     0,0,0,0,1,-1 };
int visit[65][1300][150] = {
    
     0 };
int N, M, H, T;
typedef struct node {
    
    
	int x;
	int y;
	int z;
}Pos;
bool pan(int i, int j, int k)
{
    
    
	if (i < 0 || i >= H || j < 0 || j >= N || k < 0 || k >= M)
	{
    
    
		return false;
	}
	if (visit[i][j][k] == 1)
	{
    
    
		return false;
	}
	if (graph[i][j][k] == 0)
	{
    
    
		return false;
	}
	return true;
}
int bfs(int i, int j, int k)
{
    
    
	int ans = 1;
	queue<Pos> q;
	Pos pos;
	pos.x = j;
	pos.y = k;
	pos.z = i;
	q.push(pos);
	visit[i][j][k] = 1;
	while (!q.empty())
	{
    
    
		Pos cur = q.front();
		q.pop();
		for (int o = 0; o < 6; o++)
		{
    
    
			int nextx = cur.x + dx[o];
			int nexty = cur.y + dy[o];
			int nextz = cur.z + dz[o];
			if (pan(nextz, nextx, nexty))
			{
    
    
				pos.x = nextx;
				pos.y = nexty;
				pos.z = nextz;
				q.push(pos);
				visit[nextz][nextx][nexty] = 1;
				ans++;
			}
		}
	}
	if (ans >= T)
		return ans;
	else
		return 0;
}
int main()
{
    
    
	
	cin >> N >> M >> H >> T;
	for (int i = 0; i < H; i++)
	{
    
    
		for (int j = 0; j < N; j++)
		{
    
    
			for (int k = 0; k < M; k++)
			{
    
    
				cin >> graph[i][j][k];
			}
		}
	}
	int all = 0;
	for (int i = 0; i < H; i++)
	{
    
    
		for (int j = 0; j < N; j++)
		{
    
    
			for (int k = 0; k < M; k++)
			{
    
    
				if (pan(i, j, k))
				{
    
    
                    int temp=1;
					visit[i][j][k] = 1;
					all += bfs(i, j, k);
				}
				
			}
		}
	}
	printf("%d\n", all);
}

Guess you like

Origin blog.csdn.net/qq_45478482/article/details/120117247