这道题用dfs做的话,因为递归太多层,堆栈溢出,有两个测试点过不了;所以用bfs。
根据他人代码修改后的结果:
#include <cstdio>
#include <vector>
struct node{
int height;
int width;
int length;
node(int _height, int _width, int _length): height(_height), width(_width), length(_length){}
};
int M, N, L, T, area, ans;
int x[6] = {1, -1, 0, 0, 0, 0};
int y[6] = {0, 0, 1, -1, 0, 0};
int z[6] = {0, 0, 0, 0, 1, -1};
std::vector<std::vector<std::vector<int>>> vec;
void bfs(int a, int b, int c){
std::vector<node> curr;
int pivot = 0;
int curri, currj, currk, tmpi, tmpj, tmpk;
vec[a][b][c] = 0;
curr.push_back(node(a, b, c));
while(pivot != curr.size()){
curri = curr[pivot].height;
currj = curr[pivot].width;
currk = curr[pivot].length;
for(int i = 0; i < 6; ++i){
tmpi = curri + x[i];
tmpj = currj + y[i];
tmpk = currk + z[i];
if(tmpi < 0 || tmpi >= L || tmpj < 0 || tmpj >= M || tmpk < 0 || tmpk >= N || vec[tmpi][tmpj][tmpk] == 0){
continue;
}
vec[tmpi][tmpj][tmpk] = 0;
curr.push_back(node(tmpi, tmpj, tmpk));
}
++pivot;
}
area = curr.size();
}
int main(){
scanf("%d %d %d %d", &M, &N, &L, &T);
std::vector<int> line(N, 0);
std::vector<std::vector<int>> plane(M, line);
vec.resize(L, plane);
for(int i = 0; i < L; ++i){
for(int j = 0; j < M; ++j){
for(int k = 0; k < N; ++k){
scanf("%d", &vec[i][j][k]);
}
}
}
ans = 0;
for(int i = 0; i < L; ++i){
for(int j = 0; j < M; ++j){
for(int k = 0; k < N; ++k){
if(vec[i][j][k] == 1){
bfs(i, j, k);
if(area >= T){
ans += area;
}
}
}
}
}
printf("%d", ans);
return 0;
}
比较笨拙的写法:
#include <cstdio>
#include <vector>
const int MAXM = 1286;
const int MAXN = 128;
const int MAXL = 60;
struct node{
int height;
int width;
int length;
node(int _height, int _width, int _length): height(_height), width(_width), length(_length){}
};
int M, N, L, T, area, ans;
int vec[MAXL][MAXM][MAXN];
int bfs(int a, int b, int c){
std::vector<node> curr;
int pivot = 0;
int i, j, k, tmpi, tmpj, tmpk;
vec[a][b][c] = 0;
curr.push_back(node(a, b, c));
while(pivot != curr.size()){
i = curr[pivot].height;
j = curr[pivot].width;
k = curr[pivot].length;
if(i + 1 < L && vec[i + 1][j][k] == 1){
vec[i + 1][j][k] = 0;
curr.push_back(node(i + 1, j, k));
}
if(i - 1 >= 0 && vec[i - 1][j][k] == 1){
vec[i - 1][j][k] = 0;
curr.push_back(node(i - 1, j, k));
}
if(j + 1 < M && vec[i][j + 1][k] == 1){
vec[i][j + 1][k] = 0;
curr.push_back(node(i, j + 1, k));
}
if(j - 1 >= 0 && vec[i][j - 1][k] == 1){
vec[i][j - 1][k] = 0;
curr.push_back(node(i, j - 1, k));
}
if(k + 1 < N && vec[i][j][k + 1] == 1){
vec[i][j][k + 1] = 0;
curr.push_back(node(i, j, k + 1));
}
if(k - 1 >= 0 && vec[i][j][k - 1] == 1){
vec[i][j][k - 1] = 0;
curr.push_back(node(i, j, k - 1));
}
++pivot;
}
return curr.size();
}
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", &vec[i][j][k]);
}
}
}
ans = 0;
for(int i = 0; i < L; ++i){
for(int j = 0; j < M; ++j){
for(int k = 0; k < N; ++k){
if(vec[i][j][k] == 1){
area = bfs(i, j, k);
if(area >= T){
ans += area;
}
}
}
}
}
printf("%d", ans);
return 0;
}
题目如下:
One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given the results of image analysis in which the core regions are identified in each MRI slice, your job is to calculate the volume of the stroke core.
Input Specification:
Each input file contains one test case. For each case, the first line contains 4 positive integers: M, N, Land T, where M and N are the sizes of each slice (i.e. pixels of a slice are in an M×N matrix, and the maximum resolution is 1286 by 128); L (≤60) is the number of slices of a brain; and T is the integer threshold (i.e. if the volume of a connected core is less than T, then that core must not be counted).
Then L slices are given. Each slice is represented by an M×N matrix of 0's and 1's, where 1 represents a pixel of stroke, and 0 means normal. Since the thickness of a slice is a constant, we only have to count the number of 1's to obtain the volume. However, there might be several separated core regions in a brain, and only those with their volumes no less than T are counted. Two pixels are connected and hence belong to the same region if they share a common side, as shown by Figure 1 where all the 6 red pixels are connected to the blue one.
Figure 1
Output Specification:
For each case, output in a line the total volume of the stroke core.
Sample Input:
3 4 5 2
1 1 1 1
1 1 1 1
1 1 1 1
0 0 1 1
0 0 1 1
0 0 1 1
1 0 1 1
0 1 0 0
0 0 0 0
1 0 1 1
0 0 0 0
0 0 0 0
0 0 0 1
0 0 0 1
1 0 0 0
Sample Output:
26