PAT A1091 Acute Stroke
原来读题可以这么费劲。。。感觉读题比做题难;
这道题和之前BFS所做的例题基本类似,就是一个利用BFS的遍历问题;
同样的,还是要提一下关于坐标数组的问题:
如上所示,对于一个点,在三维坐标下,有六个方向,所以组合必须有六个,从上述的数组可以看到,竖着来看,确实是有六个坐标方向,在枚举的时候要同样注意方向问题;
详细代码如下:
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<vector>
#include<queue>
using namespace std;
using std::vector;
struct node{
int ix;
int iy;
int iz;
}Node;
int m,n,slice,t;
//m,n为二维数组维数
//slice代表三维数组的高度
//t代表和的最小值
int matrix[1290][130][61];
bool vis[1290][130][761]={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};
bool charge(int x,int y,int z){
if(x>=n||x<0||y>=m||y<0||z>=slice||z<0)
return false;
if(matrix[x][y][z]==0||vis[x][y][z]==true)
return false;
return true;
}
int BFS(int x,int y,int z){
int tot=0;
queue<node>q;
Node.ix=x;
Node.iy=y;
Node.iz=z;
q.push(Node);
vis[x][y][z]=true;
while(!q.empty()){
node top=q.front();
q.pop();
tot++;
for(int i=0;i<6;i++){
int newX=top.ix+X[i];
int newY=top.iy+Y[i];
int newZ=top.iz+Z[i];
if(charge(newX,newY,newZ)){
Node.ix=newX;
Node.iy=newY;
Node.iz=newZ;
q.push(Node);
vis[newX][newY][newZ]=true;
}
}
}
if(tot>=t)
return tot;
else
return 0;
}
int main(){
scanf("%d%d%d%d",&n,&m,&slice,&t);
for(int z=0;z<slice;z++){
for(int x=0;x<n;x++){
for(int y=0;y<m;y++){
scanf("%d",&matrix[x][y][z]);
}
}
}
int ans=0;
for(int z=0;z<slice;z++){
for(int x=0;x<n;x++){
for(int y=0;y<m;y++){
if(matrix[x][y][z]==1&&vis[x][y][z]==false){
ans+=BFS(x,y,z);
}
}
}
}
printf("%d\n",ans);
system("pause");
return 0;
}