PAT1091解题报告

这是一个简单的广度优先搜索题:#include #include using namespace std;int pixel[1286][128][65];bool visit[1286][128][65];int dir[6][3] = { {0, 0, -1}, {0, 0, 1}, {-1, 0, 0}, {1, 0, 0}, {0, -1, 0}, {0, 1, 0}
摘要由CSDN通过智能技术生成

这是一个简单的广度优先搜索题:

#include <iostream>
#include <queue>
using namespace std;

int pixel[1286][128][65];
bool visit[1286][128][65];
int dir[6][3] = {
	{0, 0, -1}, {0, 0, 1},
	{-1, 0, 0}, {1, 0, 0},
	{0, -1, 0}, {0, 1, 0}};

int M, N, L, T;

typedef struct Vec3i{
	int x, y, z;
	Vec3i(){}
	Vec3i(int _x, int _y, int _z): x(_x), y(_y), z(_z){}
}Vec3i;

int ConnectCheck(Vec3i &pos){
	queue<Vec3i> Q;
	Q.push(pos);
	visit[pos.x][pos.y][pos.z] = true;
	int cnt = 1;
	while(!Q.empty()){
		Vec3i cur = Q.front();
		Q.pop();
		for(int i = 0; i < 6; ++i){
			Vec3i next;
			next.x = cur.x + dir[i][0];
			next.y = cur.y + dir[i][1];
			next.z = cur.z + dir[i][2];
			if(next.x >= 0 && next.x < N &&
				next.y >= 0 && next.y < M &&
				next.z >= 0 && next.z < L){
					if(pixel[next.x][next.y][next.z] &&
						!visit[next.x][next.y][next.z]){
						Q.push(next);
						visit[next.x][next.y][next.z] = true;
						cnt++;
					}
			}
		}
	}
	return cnt;
}

int main(){
	while(cin>>M>>N>>L>>T){
		for(int s = 0; s < L; ++s)
			for(int h = 0; h < M; ++h)
				for(int w = 0; w < N; ++w){
					cin>>pixel[w][h][s];
					visit[w][h][s] = false;
				}
		int cnt = 0;
		for(int s = 0; s < L; ++s){
			for(int h = 0; h < M; ++h){
				for(int w = 0; w < N; ++w){
					if(pixel[w][h][s] != 0 && !visit[w][h][s]){
						Vec3i pos(w, h, s);
						int _cnt = ConnectCheck(pos);
						if(_cnt >= T){
							cnt += _cnt;
						}
					}
				}
			}
		}
		cout<<cnt<<endl;
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值