A1091 Acute Stroke (30分)

#include<cstdio>
#include<stdlib.h>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<set>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
using namespace std;

int m,n,slice,Tsize; //每个切片大小是m*n 
const int maxn=2000;
//需要使用三维数组
int pixel[1290][130][61];
//根据题目要求是1286by 128 slice<=69(切片个数:也就是z) 
bool inqu[1290][130][61]={false};//记录是否访问过 
//六个方向的遍历
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};

struct node{
	int x,y,z;
}temp;

//pop的时候进行操作是访问的时候,push的时候进行操作是入队列的时候 
//判断是否需要访问
bool test(int x,int y,int z){
	//越界 
	if(x>=m||x<0||y>=n||y<0||z>=slice||z<0)return false;
	if(pixel[x][y][z]==0||inqu[x][y][z]==true) return false;
	return true;
} 

int DFS(int x,int y,int z){
	int volume=0;
	queue<node> q;
	temp.x=x,temp.y=y,temp.z=z;
	q.push(temp);
	//push时进行inqu的赋值 
	inqu[x][y][z]=true; 
	volume++;
	while(!q.empty()){
		node top=q.front();//新设一个node的变量 
		q.pop();//只有pop出去之后再改变temp的值不会影响 
		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];
			//更新的temp的节点
			if(test(newX,newY,newZ)){
				temp.x=newX;
				temp.y=newY;
				temp.z=newZ;
				q.push(temp);
				volume++;
				inqu[newX][newY][newZ]=true;
			} 
		}
		
	}
	return volume; 
}



int main()
{
	scanf("%d%d%d%d",&m,&n,&slice,&Tsize);
	for(int z=0;z<slice;z++){
		for(int x=0;x<m;x++){
			for(int y=0;y<n;y++){
				scanf("%d",&pixel[x][y][z]);
			}
		}
	}
	int totalSum=0;//计算最后的数量 
		for(int z=0;z<slice;z++){
		for(int x=0;x<m;x++){
			for(int y=0;y<n;y++){
				if(test(x,y,z)){
					int volume=DFS(x,y,z);
					if(volume>=Tsize)totalSum+=volume;
				}
			}
		}
	}

	 cout<<totalSum<<endl;
	return 0;
	}

此题把算法笔记例题看懂加上看懂题目就能写出来了,不难

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值