pat1091. Acute Stroke (30)

 

1091. Acute Stroke (30)

时间限制
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.


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

提交代码

 

 

方法一:二维数组做法。注意DFS要段错误的。

  1 #include<cstdio>
  2 #include<cmath>
  3 #include<cstring>
  4 #include<stack>
  5 #include<algorithm>
  6 #include<iostream>
  7 #include<stack>
  8 #include<set>
  9 #include<map>
 10 #include<vector>
 11 #include<queue>
 12 using namespace std;
 13 int m,n,l,t;
 14 bool isin(int x,int y,int part)
 15 {
 16     if(x<0||x/m!=part||y<0||y>=n)
 17     {
 18         return false;
 19     }
 20     return true;
 21 }
 22 bool vis[1250*60][150],ma[1250*60][150];
 23 int dx[6]= {-1,1,0,0},dy[6]= {0,0,-1,1};
 24 /*void DFS(int x,int y,int &count){
 25     int part=x/m;//块的编号
 26     vis[x][y]=true;
 27     int i,xx,yy;
 28     for(i=0;i<4;i++){
 29         xx=x+dx[i];
 30         yy=y+dy[i];
 31         if(isin(xx,yy,part)&&ma[xx][yy]&&!vis[xx][yy]){
 32             DFS(xx,yy,++count);
 33         }
 34     }
 35     xx=x+dx[i];
 36     yy=y+dy[i];
 37     if(xx>=0&&ma[xx][yy]&&!vis[xx][yy]){
 38         DFS(xx,yy,++count);
 39     }
 40     i++;
 41     xx=x+dx[i];
 42     yy=y+dy[i];
 43     if(xx<l&&ma[xx][yy]&&!vis[xx][yy]){
 44         DFS(xx,yy,++count);
 45     }
 46 }*/
 47 void BFS(int x,int y,int &count)
 48 {
 49     queue<pair<int,int> > q;
 50     vis[x][y]=true;
 51     pair<int,int> p;
 52     q.push(make_pair(x,y));
 53     int xx,yy,i;
 54     while(!q.empty())
 55     {
 56         p=q.front();
 57         q.pop();
 58         x=p.first;
 59         y=p.second;
 60         int part=x/m;
 61         for(i=0; i<4; i++)
 62         {
 63             xx=x+dx[i];
 64             yy=y+dy[i];
 65             if(isin(xx,yy,part)&&ma[xx][yy]&&!vis[xx][yy])
 66             {
 67                 vis[xx][yy]=true;
 68                 count++;
 69                 q.push(make_pair(xx,yy));//DFS(xx,yy,++count);
 70             }
 71         }
 72         xx=x+dx[i];
 73         yy=y+dy[i];
 74         if(xx>=0&&ma[xx][yy]&&!vis[xx][yy])
 75         {
 76             vis[xx][yy]=true;
 77             count++;
 78             q.push(make_pair(xx,yy));//DFS(xx,yy,++count);
 79         }
 80         i++;
 81         xx=x+dx[i];
 82         yy=y+dy[i];
 83         if(xx<l&&ma[xx][yy]&&!vis[xx][yy])
 84         {
 85             vis[xx][yy]=true;
 86             count++;
 87             q.push(make_pair(xx,yy));//DFS(xx,yy,++count);
 88         }
 89     }
 90 }
 91 int main()
 92 {
 93     //freopen("D:\\INPUT.txt","r",stdin);
 94     scanf("%d %d %d %d",&m,&n,&l,&t);
 95     l*=m;
 96     dx[4]=-m;
 97     dy[4]=0;
 98     dx[5]=m;
 99     dy[5]=0;
100     int i,j,num;
101     memset(vis,false,sizeof(vis));
102     memset(ma,false,sizeof(ma));
103     for(i=0; i<l; i++)
104     {
105         for(j=0; j<n; j++)
106         {
107             scanf("%d",&num);
108             if(num)
109             {
110                 ma[i][j]=true;
111             }
112         }
113     }
114     int count,sum=0;
115     for(i=0; i<l; i++)
116     {
117         for(j=0; j<n; j++)
118         {
119             if(ma[i][j]&&!vis[i][j])
120             {
121                 count=1;
122 
123                 BFS(i,j,count);
124                 if(count>=t)
125                 {
126                     sum+=count;
127                 }
128             }
129         }
130     }
131     printf("%d\n",sum);
132     return 0;
133 }

 

 

方法二:三维数组。

  1 #include<cstdio>
  2 #include<cmath>
  3 #include<cstring>
  4 #include<stack>
  5 #include<algorithm>
  6 #include<iostream>
  7 #include<stack>
  8 #include<set>
  9 #include<map>
 10 #include<vector>
 11 #include<queue>
 12 using namespace std;
 13 int m,n,l,t;
 14 bool isin(int x,int y,int z)
 15 {
 16     if(x<0||x>=m||y<0||y>=n||z<0||z>=l)
 17     {
 18         return false;
 19     }
 20     return true;
 21 }
 22 struct node{
 23     int x,y,z;
 24     node(){}
 25     node(int _x,int _y,int _z):x(_x),y(_y),z(_z){}
 26 };
 27 bool vis[65][1250][150],ma[65][1250][150];
 28 int dx[6]= {-1,1,0,0,0,0},dy[6]= {0,0,-1,1,0,0},dz[6]= {0,0,0,0,-1,1};
 29 void BFS(int x,int y,int z,int &count)
 30 {
 31     queue<node> q;
 32     vis[z][x][y]=true;
 33     q.push(node(x,y,z));
 34     int xx,yy,zz,i;
 35     node p;
 36     while(!q.empty())
 37     {
 38         p=q.front();
 39         q.pop();
 40         x=p.x;
 41         y=p.y;
 42         z=p.z;
 43         for(i=0; i<6; i++)
 44         {
 45             xx=x+dx[i];
 46             yy=y+dy[i];
 47             zz=z+dz[i];
 48             if(isin(xx,yy,zz)&&ma[zz][xx][yy]&&!vis[zz][xx][yy])
 49             {
 50                 vis[zz][xx][yy]=true;
 51                 count++;
 52                 q.push(node(xx,yy,zz));//DFS(xx,yy,++count);
 53             }
 54         }
 55     }
 56 }
 57 int main()
 58 {
 59     //freopen("D:\\INPUT.txt","r",stdin);
 60     scanf("%d %d %d %d",&m,&n,&l,&t);
 61     int i,j,k,num;
 62     memset(vis,false,sizeof(vis));
 63     memset(ma,false,sizeof(ma));
 64     for(k=0; k<l; k++)
 65     {
 66         for(i=0; i<m; i++)
 67         {
 68             for(j=0; j<n; j++)
 69             {
 70                 scanf("%d",&num);
 71                 if(num)
 72                 {
 73                     ma[k][i][j]=true;
 74                 }
 75             }
 76         }
 77     }
 78 
 79     int count,sum=0;
 80     for(k=0; k<l; k++)
 81     {
 82         for(i=0; i<m; i++)
 83         {
 84             for(j=0; j<n; j++)
 85             {
 86                 if(ma[k][i][j]&&!vis[k][i][j])
 87                 {
 88                     count=1;
 89                     BFS(i,j,k,count);
 90                     if(count>=t)
 91                     {
 92                         sum+=count;
 93                     }
 94                 }
 95             }
 96         }
 97     }
 98     printf("%d\n",sum);
 99     return 0;
100 }

 

posted @ 2015-09-06 20:51  Deribs4  阅读(307)  评论(0编辑  收藏  举报