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
For each case, output in a line the total volume of the stroke core.
Sample Input:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
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:
1 |
26 |
记录 stroke 的像素,再深度优先搜索以统计即可……
(有更好的思路,不过段错误了,我改过后再发上来
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
#include<stdio.h> #include<stdlib.h> #define STROKE 1 #define NORMAL 0 #define SCANED -1 typedef struct postion_ { int x; int y; int z; } postion; typedef struct stack_ { int size; int capacity; int top; postion *data; } stack; postion directions[6]={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}}; int m,n,l,t,***slices; stack *s; #define is_stack_empty(X) (!((X)->size)) #define ptr_top(X) ((X)->data[(X)->top]) stack *inti_stack(int n) { stack *temp; temp=(stack *)malloc(sizeof(stack)); temp->size=0; temp->capacity=n; temp->top=-1; temp->data=(postion *)malloc(n*sizeof(postion)); return temp; } int push(stack *s,postion data) { if(s->size==s->capacity) { return 1; } (s->top)++; (s->size)++; s->data[s->top]=data; return 0; } int pop(stack *s,postion *data) { if(s->size==0) { return 1; } *data=s->data[s->top]; (s->size)--; (s->top)--; return 0; } int reset_stack(stack *s) { if(s==NULL) { return 1; } s->size=0; s->top=-1; return 0; } int can_scan(postion pos,int direct) { int x,y,z; x=pos.x+directions[direct].x; y=pos.y+directions[direct].y; z=pos.z+directions[direct].z; if(x<0||x>=n||y<0||y>=m||z<0||z>=l||slices[z][y][x]!=STROKE) { return 0; } return 1; } int scan_this_area(postion pos) { int count=0,i; postion temp; push(s,pos); while(!is_stack_empty(s)) { pop(s,&pos); if(slices[pos.z][pos.y][pos.x]!=SCANED) { slices[pos.z][pos.y][pos.x]=SCANED; count++; } for(i=0;i<6;i++) { if(can_scan(pos,i)) { temp.x=pos.x+directions[i].x; temp.y=pos.y+directions[i].y; temp.z=pos.z+directions[i].z; push(s,temp); } } } if(count<t) { return 0; } return count; } int main() { int i,j,k,offset=0,count=0; postion *record; scanf("%d%d%d%d",&m,&n,&l,&t); slices=(int ***)malloc(l*sizeof(int **)); record=(postion *)malloc(m*n*l*sizeof(postion)); s=inti_stack(m*n*l); for(i=0;i<l;i++) { slices[i]=(int **)malloc(m*sizeof(int *)); for(j=0;j<n;j++) { slices[i][j]=(int *)malloc(n*sizeof(int)); } } for(i=0;i<l;i++) { for(j=0;j<m;j++) { for(k=0;k<n;k++) { scanf("%d",&slices[i][j][k]); if(slices[i][j][k]==STROKE) { record[offset].z=i; record[offset].y=j; record[offset].x=k; offset++; } } } } for(i=0;i<offset;i++) { if(slices[record[i].z][record[i].y][record[i].x]==STROKE) { count+=scan_this_area(record[i]); } } printf("%d\n",count); } |