首页 > 代码库 > pat1091. Acute Stroke (30)
pat1091. Acute Stroke (30)
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.
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 21 1 1 11 1 1 11 1 1 10 0 1 10 0 1 10 0 1 11 0 1 10 1 0 00 0 0 01 0 1 10 0 0 00 0 0 00 0 0 10 0 0 11 0 0 0Sample Output:
26
提交代码
题意:有l 片slices 每片上由 n*m个 像素点, l 片slices是按次序从下往上或者从上往下, 三维空间里上下左右相邻的算一块,一块至少要包含t个像素点才能计入总数,求总数
dfs就好了,不过最后两个案例数据量比较大,会爆栈,要用队列来写
#include<cstdio>#include<iostream>#include<queue>using namespace std;int m,n,l,T,volume,totVol;bool mapp[70][1300][130];struct Point{ int z,x,y; Point(){} Point(int z,int x,int y):z(z),x(x),y(y){}};void dfs(int z,int x,int y){ //此方法最后两个案例段错误,原因是递归深度会大于100000 if(mapp[z][x][y]) { volume++; mapp[z][x][y]=false; if(z>0)dfs(z-1,x,y); if(z+1<l)dfs(z+1,x,y); if(x>0)dfs(z,x-1,y); if(x+1<m)dfs(z,x+1,y); if(y>0)dfs(z,x,y-1); if(y+1<n)dfs(z,x,y+1); }}void dfs_queue(int i,int j,int k){ if(!mapp[i][j][k])return; queue<Point>p; p.push(Point(i,j,k)); while(!p.empty()) { int z=p.front().z; int x=p.front().x; int y=p.front().y; p.pop(); if(mapp[z][x][y]){ mapp[z][x][y]=false; volume++; if(z>0)p.push(Point(z-1,x,y)); if(z+1<l)p.push(Point(z+1,x,y)); if(x>0)p.push(Point(z ,x-1,y)); if(x+1<m)p.push(Point(z,x+1,y)); if(y>0)p.push(Point(z,x,y-1)); if(y+1<n)p.push(Point(z,x,y+1)); } }}int main(){ while(~scanf("%d%d%d%d",&m,&n,&l,&T)) { int x; for(int i=0;i<l;i++) { for(int j=0;j<m;j++) { for(int k=0;k<n;k++) { scanf("%d",&x); if(x)mapp[i][j][k]=true; else mapp[i][j][k]=false; } } } totVol=0; for(int i=0;i<l;i++) { for(int j=0;j<m;j++) { for(int k=0;k<n;k++) { volume=0; // dfs(i,j,k); dfs_queue(i,j,k); if(volume>=T)totVol+=volume; } } } printf("%d\n",totVol); } return 0;}
pat1091. Acute Stroke (30)