PAT (Advanced Level) Practise 1091 Acute Stroke (30)

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

求大于t的连通块的点数之和,dfs竟然会段溢出。只好用并查集。

#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1e7 + 10;
int a[62][130][1288];
int fa[maxn], cnt[maxn], n, m, l, t, ans, tot;

int get(int x)
{
	return x == fa[x] ? x : fa[x] = get(fa[x]);
}

int main()
{
	scanf("%d%d%d%d", &n, &m, &l, &t);
	for (int i = 1; i <= l; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			for (int k = 1; k <= m; k++)
			{
				scanf("%d", &a[i][j][k]);
				if (a[i][j][k])
				{
					a[i][j][k] = ++tot;
					fa[tot] = tot;
				}
			}
		}
	}
	for (int i = 1; i <= l; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			for (int k = 1, x, y; k <= m; k++)
			{
				if (!a[i][j][k]) continue;
				x = get(a[i][j][k]);
				y = get(a[i - 1][j][k]);
				if (y) fa[y] = x;
				y = get(a[i][j - 1][k]);
				if (y) fa[y] = x;
				y = get(a[i][j][k - 1]);
				if (y) fa[y] = x;
			}
		}
	}
	for (int i = 1; i <= l; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			for (int k = 1; k <= m; k++)
			{
				a[i][j][k] = get(a[i][j][k]);
				cnt[a[i][j][k]]++;
			}
		}
	}
	for (int i = 1; i <= tot; i++) if (cnt[i] >= t) ans += cnt[i];
	printf("%d\n", ans);
	return 0;
}


你可能感兴趣的:(pat)