1. 程式人生 > >PAT 1091. Acute Stroke (30)

PAT 1091. Acute Stroke (30)

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.

Figure 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

思路
這道題如果破解了英文“密碼”,實際就是一道求三維矩陣裡連通路徑的問題!不過特意做題解是因為有個非常簡便的搜尋方法:用dx,dy,dz三個向量來寫優雅的程式碼
vector<int> dx {1,-1,0,0,0,0};
vector<int> dy {0,0,1,-1,0,0};
vector<int> dz {0,0,0,0,1,-1};
//對於一個點,要遍歷其周圍上下左右前後六個方向,只需要一個迴圈
point p(x,y,z);
int xx = p.x;
int yy = p.y;
int zz = p.z;
for(int i=0 ; i<6 ; i++){
    visit(point(xx+dx[i],yy+dy[i],zz+dz[i]);
}

下面是參考了這位牛人的blog的程式碼

#include<iostream>
#include<algorithm>
#include<vector>
#include<fstream>
#include<limits.h>
#include<queue> 
using namespace std;

int n,m,s,t;
vector<vector<vector<int > > > map;
int ret = 0;//result
bool inRange(int& x, int& y, int& z){
    return ((x<n)&&(x>=0)&&(y<m)&&(y>=0)&&(z<s)&&(z>=0));
}

struct loc{
    int x;
    int y;
    int z;
    loc(int x, int y, int z):x(x),y(y),z(z){}
};

int dx[6] = {1,-1,0,0,0,0};
int dy[6] = {0,0,1,-1,0,0};
int dz[6] = {0,0,0,0,1,-1};

void bfs(int x,int y, int z){
    queue<loc>  q;
    int ans = 0;
    q.push(loc(x,y,z));
    ans++;
    map[x][y][z] = 0;

    while(!q.empty()){
        int xx = q.front().x;
        int yy = q.front().y;
        int zz = q.front().z;
        q.pop();

        for(int i = 0 ; i<6 ; i++){
            int nx = xx+dx[i];
            int ny = yy+dy[i];
            int nz = zz+dz[i];
            if (inRange(nx,ny,nz )&&map[nx][ny][nz]){
                map[nx][ny][nz] = 0;
                q.push(loc(nx,ny,nz));
                ans++;
            }
        }
    }

    if (ans>=t)
        ret+=ans;
}

int main(){
    cin >> n >> m >> s >> t;
    map.resize(n);
    for (int i=0 ; i<n ; i++){
        map[i].resize(m);
    }
    for (int i=0 ; i<n ; i++){
        for (int j=0 ; j<m ; j++){
            map[i][j].resize(s,0);
        }
    }
    for (int k=0 ; k<s ; k++){
        for (int i=0 ; i<n ; i++){
            for (int j=0 ; j<m ; j++){
                cin >> map[i][j][k];
            }
        }
    }

    for (int k=0 ; k<s ; k++){
        for (int i=0 ; i<n ; i++){
            for (int j=0 ; j<m ; j++){
                if (map[i][j][k])
                    bfs(i,j,k);
            }
        }
    }
    cout << ret;

    return 0;
}