Not Only Algorithm,不仅仅是算法,关注数学、算法、数据结构、程序员笔试面试以及一切涉及计算机编程之美的内容 。。
你的位置:NoAlGo博客 » 题解 » 

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

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>
#include <queue>
using namespace std;

struct Node 
{
	int x, y, z; 
	Node (int a = 0, int b = 0, int c = 0) : x(a), y(b), z(c) {}
};
queue<Node> que;
const int mxx = 1300, mxy = 130, mxz = 70;
int m, n, l, t;
bool g[mxx][mxy][mxz], vs[mxx][mxy][mxz] = { 0 };
int dx[] = {0, -1, 0, 1, 0, 0}, dy[] = {-1, 0, 1, 0, 0, 0}, dz[] = {0, 0, 0, 0, 1, -1};

int main()
{
	scanf("%d%d%d%d", &m, &n, &l, &t);
	for (int k = 0; k < l; k++)
		for (int i = 0; i < m; i++)
			for (int j = 0; j < n; j++)
				scanf("%d", &g[i][j][k]);
	int ans = 0;
	for (int i = 0; i < m; i++)
		for (int j = 0; j < n; j++)
			for (int k = 0; k < l; k++)
				if (!vs[i][j][k] && g[i][j][k])
				{
					int cnt = 1;
					que.push(Node(i, j, k)); vs[i][j][k] = 1; //ฟํหั
					while(!que.empty())
					{
						Node top = que.front(); que.pop();
						for (int ii = 0; ii < 6; ii++)
						{
							int tx = top.x + dx[ii], ty = top.y + dy[ii], tz = top.z + dz[ii];
							if (tx>=0 && tx<m && ty>=0 && ty<n && tz>=0 && tz<l && !vs[tx][ty][tz] && g[tx][ty][tz])
								que.push(Node(tx, ty, tz)), vs[tx][ty][tz] = 1, cnt++;
						}
					}
					if (cnt >= t) ans += cnt;
				}
	printf("%d\n", ans);
}
上一篇: 下一篇:

我的博客

NoAlGo头像编程这件小事牵扯到太多的知识,很容易知其然而不知其所以然,但真正了不起的程序员对自己程序的每一个字节都了如指掌,要立足基础理论,努力提升自我的专业修养。

站内搜索

最新评论