zoukankan      html  css  js  c++  java
  • PAT Advanced 1091 Acute Stroke (30) [⼴度优先搜索BFS]

    题目

    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

    题目分析

    三维数组,数组元素的取值为0或1,相邻元素组成块,块中1的个数大于阈值称为卒中核心区,广度优先遍历,统计每个卒中核心区1的个数之和。

    解题思路

    三维数组广度遍历(注:深度遍历能实现但是会超时,深度过深,导致栈溢出)
    六个方向坐标增量使用数组表示:X Y Z

    Code

    #include <iostream>
    #include <queue>
    using namespace std;
    int pixel[1290][130][61];
    int inq[1290][130][61];
    int m,n,l,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};
    struct node {
    	int x,y,z;
    } nd;
    bool judge(int x,int y,int z) {
    	if(x>=m||x<0||y>=n||y<0||z>=l||z<0)return false;
    	if(pixel[x][y][z]==0||inq[x][y][z]==1)return false;
    	return true;
    }
    int bfs(int x, int y, int z) {
    	int cnt=0;
    	queue<node> q;
    	nd.x=x,nd.y=y,nd.z=z;
    	q.push(nd);
    	inq[x][y][z]=1;
    	while(!q.empty()) {
    		node top=q.front();
    		q.pop();
    		cnt++;
    		for(int i=0; i<6; i++) {
    			int newx= top.x+X[i];
    			int newy= top.y+Y[i];
    			int newz= top.z+Z[i];
    			if(judge(newx,newy,newz)) {
    				nd.x=newx,nd.y=newy,nd.z=newz;
    				q.push(nd);
    				inq[newx][newy][newz]=1;
    			}
    		}
    	}
    	if(cnt>=t)return cnt; 
    	return 0;
    }
    int main(int argc,char * argv[]) {
    	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",&pixel[j][k][i]);
    			}
    		}
    	}
    	int ant=0;
    	for(int i=0; i<l; i++) {
    		for(int j=0; j<m; j++) {
    			for(int k=0; k<n; k++) {
    				if(pixel[j][k][i]==1&&inq[j][k][i]==0) {
    					ant+=bfs(j,k,i);
    				}
    			}
    		}
    	}
    	printf("%d",ant);
    	return 0;
    }
    

  • 相关阅读:
    《小C QQ空间转帖、分享工具》之QQ空间数据传递的g_tk算法(C#)
    2.线性回归
    1(3).频率派 VS 贝叶斯派
    sklearn---SVM
    sklearn总览
    word转pdf时图片质量下降的解决方案
    python-字符串前面添加u,r,b的含义
    matplotlib---保存图片出现的问题
    matplotlib---设置线条颜色及形状
    numpy中arange()和linspace()区别
  • 原文地址:https://www.cnblogs.com/houzm/p/12513675.html
Copyright © 2011-2022 走看看