1091 Acute Stroke (30分)[BFS]

原题链接
广度优先探索 还是比较有套路的
我错主要是错在了cnt++的位置
要在弹出去之前cnt++ 还有要注意深度防爆栈inq[1290][130][61]
AC代码:

#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
const int maxn = 1290;
struct node
{
	int x, y, z;
}Node;

int X[6] = {0,0,0,0,1,-1};
int Y[6] = {0,0,-1,1,0,0};
int Z[6] = {1,-1,0,0,0,0};

int m, n, clice, t, ans = 0;
bool inq[1290][130][61] = {false}; //判断是否在队列里
int pixel[1290][130][61];
bool judge(int x, int y, int z) //n行m列lice层
{
	if(x >= n || x < 0 || y >= m || y < 0 || z < 0 || z >= clice) return false;
    if(pixel[x][y][z] == 0 || inq[x][y][z]) return false;
    return true;
}
int BFS(int x, int y, int z) //进队,如队列
{
	int cnt = 0;
	queue<node> q; //定义队列
	Node.x = x;
	Node.y = y;
	Node.z = z;
	q.push(Node);
	inq[x][y][z] = true;
	while(!q.empty())
	{
		node top = q.front();
		q.pop();
		cnt++; //当前块中1的个数加一
		for(int i = 0; i < 6; i++)
		{
			int newx = top.x + X[i];
			int newy = top.y + Y[i];
			int newz = top.z + Z[i];
			if(judge(newx,newy,newz))
			{
				Node.x = newx;
				Node.y = newy;
				Node.z = newz;
				q.push(Node);
				inq[newx][newy][newz] = true;
			}
		}
	}
	return cnt;
}

int main()
{
	int ans = 0;
	cin >> n >> m >> clice >> t;
	for(int z = 0; z < clice; z++)
	{
		for(int x = 0; x < n; x++)
		{
			for(int y = 0; y < m; y++)
			{
				cin >> pixel[x][y][z];
			}
		}
	}
	int temp;
	for(int z = 0; z < clice; z++)
	{
		for(int x = 0; x < n; x++)
		{
			for(int y = 0; y < m; y++)
			{
				if(pixel[x][y][z] == 1 && inq[x][y][z] == false)
				{
				     temp = BFS(x,y,z);
				     if(temp >= t) ans += temp;
				}
			}
		}
	}
	cout << ans;
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

moumoumouwang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值