本文主要是介绍PAT-1091 Acute Stroke,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1091 Acute Stroke (30)(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 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
三维图像连通域问题,其实就是找图的连通分量。开始时直接用并查集超时了,too naive .....尝试过递归的DFS(代码少好写),有两个测试点超时,应该是系统栈爆掉了。故还是用非递归BFS遍历,当然也可以用非递归的DFS,不过感觉麻烦些。
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<queue>
using namespace std;
struct point{int m,n,l;point(int _m,int _n,int _l){m=_m;n=_n;l=_l;}
};
bool pic[1286][128][60];
bool visited[1286][128][60]={0};
int t1[6]={1,-1,0,0,0,0};
int t2[6]={0,0,1,-1,0,0};
int t3[6]={0,0,0,0,1,-1};int M,N,L,T;
int area;
int total=0;
void bfs(point p){queue<point> q;q.push(p);area++;visited[p.m][p.n][p.l]=true;while(!q.empty()){point t=q.front();q.pop();for(int i=0;i<6;i++){int dx=t.m+t1[i];int dy=t.n+t2[i];int dz=t.l+t3[i];if(dx<M&&dx>=0&&dy<N&&dy>=0&&dz<L&&dz>=0&&!visited[dx][dy][dz]&&pic[dx][dy][dz]){area++;visited[dx][dy][dz]=true;q.push(point(dx,dy,dz)); }}}
}
int main(){scanf("%d %d %d %d",&M,&N,&L,&T);int tmp,num=0;for(int i=0;i<L;i++){for(int j=0;j<M;j++){for(int k=0;k<N;k++){scanf("%d",&tmp);if(tmp==1) pic[j][k][i]=1;else pic[j][k][i]=0;}}}for(int i=0;i<L;i++){for(int j=0;j<M;j++){for(int k=0;k<N;k++){if(!visited[j][k][i]&&pic[j][k][i]){area=0;bfs(point(j,k,i));if(area>=T)total+=area;}}}}printf("%d",total);return 0;
}
这篇关于PAT-1091 Acute Stroke的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!