L3-004 肿瘤诊断

首先,这是一个三维空间上的,题目的意思就是给了五个3*4的切片,从下往上叠在一起,对于一个像素点来说,他的前后左右上下这六个方向都是他的连通像素点。

代码:

#include <bits/stdc++.h>
using namespace std;
int a[65][130][1300];
int dir[6][3]={{-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1}};
int m,n,l,t,area;
struct Node{
	int x,y,z;
};
bool check(int x,int y,int z){
    if(x>=1&&x<=l&&y>=1&&y<=m&&z>=1&&z<=n){
    	return true;
	}
	return false;
} 
void BFS(int x,int y,int z){
     queue<Node> q;
     int count=1;
     Node node;
     node.x = x;
     node.y = y;
     node.z = z;
     a[x][y][z]=0;//已经来过 
     q.push(node);
     while(!q.empty()){
     	Node tmp = q.front();
     	q.pop();
     	for(int i=0;i<6;i++){
     		int xx = tmp.x + dir[i][0];
     		int yy = tmp.y + dir[i][1];
     		int zz = tmp.z + dir[i][2];
     		if(check(xx,yy,zz) && a[xx][yy][zz]){
     			a[xx][yy][zz]=0;//已经来过 
     			node.x=xx;
     			node.y=yy;
     			node.z=zz;
     			q.push(node);
     			count++;
			}
		 }
	 }
	 if(count>=t){
	 	area+=count;
	 }
}
int main(){
	cin>>m>>n>>l>>t;
	for(int i=1;i<=l;i++){
		for(int j=1;j<=m;j++){
			for(int k=1;k<=n;k++){
				cin>>a[i][j][k];
			}
		}
	}
	for(int i=1;i<=l;i++){
		for(int j=1;j<=m;j++){
			for(int k=1;k<=n;k++){
				if(a[i][j][k]){
					BFS(i,j,k);
				}
			}
		}
	}  
	cout << area << '\n';
	return 0;
}

或者((#.#)没任何区别就是count位置动一下)

#include <bits/stdc++.h>
using namespace std;
int a[65][130][1300];
int dir[6][3]={{-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1}};
int m,n,l,t,area;
struct Node{
	int x,y,z;
};
bool check(int x,int y,int z){
    if(x>=1&&x<=l&&y>=1&&y<=m&&z>=1&&z<=n){
    	return true;
	}
	return false;
} 
void BFS(int x,int y,int z){
     queue<Node> q;
     int count=0;
     Node node;
     node.x = x;
     node.y = y;
     node.z = z;
     a[x][y][z]=0;//已经来过 
     q.push(node);
     while(!q.empty()){
     	Node tmp = q.front();
     	q.pop();
     	count++;
     	for(int i=0;i<6;i++){
     		int xx = tmp.x + dir[i][0];
     		int yy = tmp.y + dir[i][1];
     		int zz = tmp.z + dir[i][2];
     		if(check(xx,yy,zz) && a[xx][yy][zz]){
     			a[xx][yy][zz]=0;//已经来过 
     			node.x=xx;
     			node.y=yy;
     			node.z=zz;
     			q.push(node);
			}
		 }
	 }
	 if(count>=t){
	 	area+=count;
	 }
}
int main(){
	cin>>m>>n>>l>>t;
	for(int i=1;i<=l;i++){
		for(int j=1;j<=m;j++){
			for(int k=1;k<=n;k++){
				cin>>a[i][j][k];
			}
		}
	}
	for(int i=1;i<=l;i++){
		for(int j=1;j<=m;j++){
			for(int k=1;k<=n;k++){
				if(a[i][j][k]){
					BFS(i,j,k);
				}
			}
		}
	}  
	cout << area << '\n';
	return 0;
}
posted @ 2024-04-03 10:41  YuKiCheng  阅读(10)  评论(0编辑  收藏  举报