1091 Acute Stroke (30 分)
解题思想
AC代码
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
int m, n, l, t;
int arr[1300][130][80];
bool visit[1300][130][80];
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};
struct node{
int x, y, z;
};
bool judge(int x, int y, int z){
if (x<0||x>=m||y<0||y>=n||z<0||z>=l) return false;
if (arr[x][y][z] == 0 || visit[x][y][z] == true) return false;
return true;
}
int bfs(int x, int y, int z){
int cnt = 0;
node tmp;
tmp.x = x;
tmp.y = y;
tmp.z = z;
queue<node> q;
q.push(tmp);
visit[x][y][z] = true;
while (!q.empty()){
node top = q.front();
q.pop();
cnt++;
for (int i = 0; i < 6; i++){
int tx = top.x + X[i];
int ty = top.y + Y[i];
int tz = top.z + Z[i];
if (judge(tx, ty, tz)){
visit[tx][ty][tz] = true;
tmp.x = tx;
tmp.y = ty;
tmp.z = tz;
q.push(tmp);
}
}
}
if (cnt >= t) return cnt;
else return 0;
}
int main(){
scanf("%d%d%d%d", &m, &n, &l, &t);
for (int i = 0; i < l; i++)
for (int j = 0; j < m; j++)
for (int k = 0; k < n; k++)
scanf("%d", &arr[j][k][i]);
int ans = 0;
for (int i = 0; i < l; i++)
for (int j = 0; j < m; j++)
for (int k = 0; k < n; k++)
if (arr[j][k][i] == 1 && visit[j][k][i] == false)
ans += bfs(j, k, i);
printf("%d", ans);
return 0;
}