1091 Acute Stroke (30)

#include<iostream>
#include<queue>
using namespace std;
struct Node{
	int x,y,z;
};
int m,n,l,t;
int a[61][1287][129]={};
bool vis[61][1287][129]={false};
int _x[6]={1,0,0,-1,0,0};
int _y[6]={0,1,0,0,-1,0};
int _z[6]={0,0,1,0,0,-1};
bool judge(int x,int y,int z){
	if(x>=l||y>=m||z>=n||x<0||y<0||z<0) return false;
	else if(a[x][y][z]==0||vis[x][y][z]==true) return false;
	else return true;
}
int bfs(int xx,int yy,int zz){
	queue<Node> q;
	vis[xx][yy][zz]=true;
	Node temp;
	temp.x=xx;
	temp.y=yy;
	temp.z=zz;
	q.push(temp);
	int cnt=0;
	while(!q.empty()){
		Node now=q.front();
		q.pop();
		cnt++;
		for(int i=0;i<6;i++){
		  Node t;
			t.x=now.x+_x[i];t.y=now.y+_y[i];t.z=now.z+_z[i];
			if(judge(t.x,t.y,t.z)){
				vis[t.x][t.y][t.z]=true;
				q.push(t);
			}
		}
	}
	if(cnt>=t){
		return cnt;
	}else return 0;
}
int main(){
	/*#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
	#endif*/
	//cin>>m>>n>>l>>t;
	scanf("%d %d %d %d",&m,&n,&l,&t);
	for(int s=0;s<l;s++){
		for(int r=0;r<m;r++){
			for(int c=0;c<n;c++){
				//cin>>a[s][r][c];
				scanf("%d",&a[s][r][c]);
			}
		}
	}
	int ans=0;
	for(int s=0;s<l;s++){
		for(int r=0;r<m;r++){
			for(int c=0;c<n;c++){
				if(vis[s][r][c]==false&&a[s][r][c]==1){
					ans+=bfs(s,r,c);
				}
				
			}
		}
	}
	//cout<<ans<<endl;
	printf("%d",ans);
	return 0;
}

本来打算暴力把所有的情况都列出来的,后来写到第三个就写不下去了,掐指一算要写十几个判断语句。。。参考了https://www.liuchuo.net/archives/2307的文章,采用bfs解决问题着实优秀,但是后来发现cin,cout两个超时,改成scanf和printf之后还有一个超时,再把平时常用的文件流删掉就AC了。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值