PTA:L3-004 肿瘤诊断

PTA:L3-004 肿瘤诊断

题解:三维BFS。注意坐标的建立。

代码:

#include <bits/stdc++.h>
using namespace std;
int const N = 200 + 10;
int n,m,h,t;
int w[N][N][N],vis[N][N][N];
int dir[6][3] ={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
struct Node
{
	int z,x,y;    
	Node(int a,int b,int c):z(a),x(b),y(c){};
};

int bfs(int z,int x,int y){
	queue<Node>q;
	q.push(Node(z,x,y));
	vis[z][x][y] = true;
	w[z][x][y] = 0;
	int ans = 1;
	while(!q.empty()){
		Node p = q.front();		q.pop();	
		for(int i=0;i<6;i++){
			int x = p.x + dir[i][0],	y = p.y + dir[i][1],	z = p.z + dir[i][2];
			if(x < 0 || x >= m || y < 0 || y >= n || z < 0 || z >= h || !w[z][x][y] || vis[z][x][y])	continue;
			vis[z][x][y] = true;
			w[z][x][y] = 0;
			ans++;
			q.push(Node(z,x,y));
		}
	}
	return ans >= t ? ans : 0;
}
int main(){
	scanf("%d%d%d%d",&m,&n,&h,&t);
	int ans = 0;
    for(int k=0;k<h;k++)
        for(int i=0;i<m;i++)
            for(int j=0;j<n;j++)
                scanf("%d",&w[k][i][j]);
    for(int k=0;k<h;k++)
        for(int i=0;i<m;i++)
            for(int j=0;j<n;j++)
                if(w[k][i][j] && !vis[k][i][j])
					ans += bfs(k,i,j);
	cout<<ans<<endl;
}

 

posted @ 2019-03-10 11:09  月光下の魔术师  阅读(23)  评论(0)    收藏  举报