1091. Acute Stroke (30)

题目链接:http://www.patest.cn/contests/pat-a-practise/1091
题目:

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, L and T, where M and N are the sizes of each slice (i.e. pixels of a slice are in an M by 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 by 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.

1091. Acute Stroke (30)_第1张图片
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

分析:
这一次理论上用BFS和DFS等搜索算法都OK,但是DFS递归栈调用太深了,会造成段错误,所以要用BFS。
AC代码:
刚开始用DFS,应该是递归栈太深了,造成了段错误。
#include<cstdio>
#include<iostream>
using namespace std;
int n, m, l, t;//DFS中要做判断,所以要设置成全局函数
int matrix[1287][129][61];
int direction[][3] = {
 0, 0, 1,
 0, 1, 0,
 1, 0, 0,
 0, 0, -1,
 0, -1, 0,
 -1, 0, 0
};
int sum = 0;
int DFS(int x, int y, int z){
 int all = 1;
 for (int i = 0; i < 6; ++i){
  int nx = x + direction[i][0];
  int ny = y + direction[i][1];
  int nz = z + direction[i][2];
  if (nx < 0 || ny < 0 || nz < 0 || nx >= m || ny >= n || nz >= l)continue;
  if (matrix[nx][ny][nz] == 0)continue;
  matrix[nx][ny][nz] = 0;
  all += DFS(nx, ny, nz);
 }
 return all;
}
int main(){
 //freopen("F://Temp/input.txt", "r", stdin);
 cin >> n >> m >> l >> t;
 for (int i = 0; i < l; ++i){
  for (int j = 0; j < n; ++j){
   for (int k = 0; k < m; ++k){//注意这里的内外层循环的变量,是l,n,m
    cin >> matrix[k][j][i];//注意这里的下标,是k,j,i
   }
  }
 }
 for (int i = 0; i < l; ++i){
  for (int j = 0; j < n; ++j){
   for (int k = 0; k < m; ++k){//这里也注意内外层循环变量,同上
    if (matrix[k][j][i] == 1){
     matrix[k][j][i] = 0;
     int temp = DFS(k,j,i);
     if (temp >= t)
      sum += temp;
    }
   }
  }
 }
 cout << sum << endl;
 return 0;
}
后来改用了BFS,终于通过了
#include<cstdio>
#include<iostream>
#include<queue>
using namespace std;
int n, m, l, t;//DFS中要做判断,所以要设置成全局函数
int matrix[1287][129][61];
int direction[][3] = {
 0, 0, 1,
 0, 1, 0,
 1, 0, 0,
 0, 0, -1,
 0, -1, 0,
 -1, 0, 0
};
struct position{
 int x, y, z;
 position(int a, int b, int c) :x(a), y(b), z(c){}
};
int sum = 0;
int BFS(position* po){
 queue<position*>Q;
 while (!Q.empty())Q.pop();
 Q.push(po);
 int all = 0;
 while (!Q.empty()){
  position* pfront = Q.front();
  Q.pop();
  all++;
  for (int i = 0; i < 6; ++i){
   int nx = pfront->x + direction[i][0];
   int ny = pfront->y + direction[i][1];
   int nz = pfront->z + direction[i][2];
   if (nx < 0 || ny < 0 || nz < 0 || nx >= m || ny >= n || nz >= l)continue;
   if (matrix[nx][ny][nz] == 0)continue;
   matrix[nx][ny][nz] = 0;
   position* p_tmp = new position(nx, ny, nz);
   Q.push(p_tmp);
  }
 }
 return all;
}
int main(){
 //freopen("F://Temp/input.txt", "r", stdin);
 cin >> n >> m >> l >> t;
 for (int i = 0; i < l; ++i){
  for (int j = 0; j < n; ++j){
   for (int k = 0; k < m; ++k){//注意这里的内外层循环的变量,是l,n,m
    cin >> matrix[k][j][i];//注意这里的下标,是k,j,i
   }
  }
 }
 for (int i = 0; i < l; ++i){
  for (int j = 0; j < n; ++j){
   for (int k = 0; k < m; ++k){//这里也注意内外层循环变量,同上
    if (matrix[k][j][i] == 1){
     matrix[k][j][i] = 0;
     position* po = new position(k, j, i);
     int temp = BFS(po);
     if (temp >= t)
      sum += temp;
    }
   }
  }
 }
 cout << sum << endl;
 return 0;
}



截图:
1091. Acute Stroke (30)_第2张图片
1091. Acute Stroke (30)_第3张图片

——Apie陈小旭

你可能感兴趣的:(DFS,pat,bfs)