PAT A1091. Acute Stroke (30)

原文链接: PAT A1091. Acute Stroke (30)

上一篇: PAT A1103. Integer Factorization (30)

下一篇: PAT A1020. Tree Traversals (25)

1091. Acute Stroke (30)

https://www.patest.cn/contests/pat-a-practise/1091

时间限制

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.

31133025_cYWM.jpg
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

设置map中的值表示是否需要访问,简化判断

使用类的概念简化编码

#include <iostream>
#include <cstdio>
#include <queue>

using namespace std;
int n, m, slice, t;
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 };
int map[1290][130][61]; 	//存放数据的三维矩阵

struct Node {
	int x, y, z;
	Node() {}
	Node(int x, int y, int z) :x(x), y(y), z(z) {}
	//判断该点是否需要被访问 
	int judge() {
		//越界 
		if (x >= n || x < 0 || y >= m || y < 0 || z >= slice || z < 0) return 0;
		//直接返回值  0,1
		return map[x][y][z];
	}

	//设置该店已经被访问 
	void visit() {
		map[x][y][z] = 0;
	}
};


int bfs(int x, int y, int z) {
	int count = 0;	//记录当前块中1的个数
	queue<Node> q;
	Node node(x, y, z);
	q.push(node);
	node.visit();
	while (!q.empty()) {
		Node top = q.front();
		q.pop();
		count++;		//当前块中1的个数加一
		for (int i = 0; i < 6; i++) {
			Node temp(top.x + X[i], top.y + Y[i], top.z + Z[i]);
			if (temp.judge()) {
				q.push(temp);
				temp.visit();
			}
		}
	}
	if (count >= t) return count;
	return 0;
}

int main(int argc, char *argv[]) {
	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", &map[x][y][z]);
	int ans = 0;
	for (int z = 0; z < slice; z++)
		for (int x = 0; x < n; x++)
			for (int y = 0; y < m; y++)
				if (map[x][y][z])
					ans += bfs(x, y, z);

	printf("%d\n", ans);
	return 0;
}

题目大意:给定一个三维数组,0表示正常1表示有肿瘤,肿瘤块的大小大于等于t才算作是肿瘤,让计算所有满足肿瘤块的大小
分析:三维的广度优先搜索~~XYZ三个数组判断方向,对每一个点广度优先累计肿瘤块的大小,如果大于等于t就把结果累加。用visit数组标记当前的点有没有被访问过,被访问过的结点是不会再访问的。。judge判断是否超过了边界,或者是否当前结点为0不是肿瘤~~~

#include <cstdio>
#include <queue>
using namespace std;
struct node {
    int x, y, z;
};
int m, n, l, t;
int X[6] = {1, 0, 0, -1, 0, 0};
int Y[6] = {0, 1, 0, 0, -1, 0};
int Z[6] = {0, 0, 1, 0, 0, -1};
int arr[1300][130][80];
bool visit[1300][130][80];
bool judge(int x, int y, int z) {
    if(x < 0 || x >= m || y < 0 || y >= n || z < 0 || z >= l) return false;
    if(arr[x][y][z] == 0 || visit[x][y][z] == true) return false;
    return true;
}
int bfs(int x, int y, int z) {
    int cnt = 0;
    node temp;
    temp.x = x, temp.y = y, temp.z = z;
    queue<node> q;
    q.push(temp);
    visit[x][y][z] = true;
    while(!q.empty()) {
        node top = q.front();
        q.pop();
        cnt++;
        for(int i = 0; i < 6; i++) {
            int tx = top.x + X[i];
            int ty = top.y + Y[i];
            int tz = top.z + Z[i];
            if(judge(tx, ty, tz)) {
                visit[tx][ty][tz] = true;
                temp.x = tx, temp.y = ty, temp.z = tz;
                q.push(temp);
            }
        }
    }
    if(cnt >= t)
        return cnt;
    else
        return 0;
    
}
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", &arr[j][k][i]);
    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(arr[j][k][i] == 1 && visit[j][k][i] == false)
                    ans += bfs(j, k, i);
            }
        }
    }
    printf("%d", ans);
    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值