Topic address: https://www.patest.cn/contests/gplt/L3-004
Tumor diagnosis
Calculating the volume of tumors is an important step in the diagnosis of tumors. Given the suspected tumor area marked in the scanning section of the lesion, please calculate the volume of the tumor.
Input format:
Input the first line gives four positive integers: MM, NN, LL, TT, where MM and NN are the dimensions of each slice (that is, each slice is an M times) NM * N pixel matrix. Maximum resolution is 1286 times 1281286×128);LL(\le 60 < 60) is the number of slices; TT is an integer threshold (if the volume of the connective body of the suspected tumor is smaller than TT, the patch is ignored).
Finally, LL slices are given. Each sheet consists of a M times consisting of 0 and 1. The matrix representation of NM * N, where 1 represents the pixel of suspected tumor and 0 represents the normal pixel. Since the slice thickness can be considered as a constant, we can get the volume as long as the number of 1 in the number connector. The trouble is that there may be multiple tumors, and we only count those that are not less than TT. Two pixels are considered "connected" if they have a common section, as shown in the figure below, all six red pixels are connected to blue pixels.
Output format:
Output of the total volume of tumors in a row.
Input sample:
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
Output sample:
26
I don't know why there are two cases with DFS in this question. Did it explode the system stack? I wonder if the Great God can tell me. Thank you.
DFS Code:
#include <iostream> #include <cstdio> #include <cmath> #include <queue> #include <stack> #include <map> #include <algorithm> #include <vector> #include <string> #include <cstring> #include <sstream> using namespace std; int pic[80][1300][150]; bool vis[80][1300][150]; int m,n,h,T; int cnt; int di[10]={-1,1,0,0,0,0}; int dj[10]={0,0,-1,1,0,0}; int dk[10]={0,0,0,0,1,-1}; void dfs(int i,int j,int k) { if(i<0||i>=h||j<0||j>=m||k<0||k>=n) return; if(vis[i][j][k]==true) return; if(pic[i][j][k]==0) return; vis[i][j][k]=true; cnt++; for(int a=0; a<6; a++) { int x=i+di[a]; int y=j+dj[a]; int z=k+dk[a]; dfs(x,y,z); } } int main() { memset(vis,false,sizeof(vis)); scanf("%d%d%d%d",&m,&n,&h,&T); for(int i=0;i<h;i++) { for(int j=0;j<m;j++) { for(int k=0;k<n;k++) { scanf("%d",&pic[i][j][k]); } } } int ans=0; for(int i=0;i<h;i++) { for(int j=0;j<m;j++) { for(int k=0;k<n;k++) { if(vis[i][j][k]==false&&pic[i][j][k]==1) { cnt=0; dfs(i,j,k); if(cnt>=T) { ans+=cnt; } } } } } printf("%d\n",ans); return 0; }
BFS Code:
#include <iostream> #include <cstdio> #include <cmath> #include <queue> #include <stack> #include <map> #include <algorithm> #include <vector> #include <string> #include <cstring> #include <sstream> using namespace std; int pic[80][1300][150]; bool vis[80][1300][150]; int m,n,h,T; int cnt; int di[10]={-1,1,0,0,0,0}; int dj[10]={0,0,-1,1,0,0}; int dk[10]={0,0,0,0,1,-1}; int ans; struct note { int i,j,k; note(int x,int y,int z) { i=x; j=y; k=z; } }; typedef struct note note; queue<note> Q; void bfs(int i,int j,int k) { note tmp(i,j,k); Q.push(tmp); while(!Q.empty()) { note a=Q.front(); Q.pop(); if(a.i>=0&&a.i<h&&a.j>=0&&a.j<m&&a.k>=0&&a.k<n) { if(vis[a.i][a.j][a.k]==false && pic[a.i][a.j][a.k]==1) { vis[a.i][a.j][a.k]=true; cnt++; for(int c=0; c<6; c++) { int x=a.i+di[c]; int y=a.j+dj[c]; int z=a.k+dk[c]; if(x>=0&&y>=0&&z>=0&&x<h&&y<m&&z<n&&vis[x][y][z]==false&&pic[x][y][z]==1) { note temp(x,y,z); Q.push(temp); } } } } } } int main() { memset(vis,false,sizeof(vis)); scanf("%d%d%d%d",&m,&n,&h,&T); for(int i=0;i<h;i++) { for(int j=0;j<m;j++) { for(int k=0;k<n;k++) { scanf("%d",&pic[i][j][k]); } } } for(int i=0;i<h;i++) { for(int j=0;j<m;j++) { for(int k=0;k<n;k++) { if(vis[i][j][k]==false&&pic[i][j][k]==1) { cnt=0; bfs(i,j,k); if(cnt>=T) { ans+=cnt; } } } } } printf("%d\n",ans); return 0; }