您的位置:首页 > 理论基础 > 数据结构算法

Acute Stroke

2016-09-04 12:43 211 查看


Acute Stroke (30)

时间限制 1000 ms 内存限制 655360 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小)

题目描述

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.


输入描述:

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

输出描述:

For each case, output in a line the total volume of the stroke core.


输入例子:

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




输出例子:

26



解答:

/* 非常好的方法----广度优先搜索*/

#include <iostream>

#include <queue>

using namespace std;

#define LMAX 60

#define MMAX 1280

#define NMAX 128

int matrix[LMAX][MMAX][NMAX];

int M, N, L, T, total = 0;

int xd[] = { 1,-1,0,0,0,0 };

int yd[] = { 0,0,1,-1,0,0 };

int zd[] = { 0,0,0,0,1,-1 };

class node

{

public :
int x, y, z;
node(int _x, int _y, int _z) :x(_x), y(_y), z(_z){
}

};

void bfs(int x, int y, int z)

{
if (matrix[x][y][z] == 0)
return;

queue<node>que;
int volumn = 1;
que.push(node(x, y, z));
matrix[x][y][z] = 0;

int i;
while (!que.empty())
{
node tem = que.front();
que.pop();
for (i = 0; i < 6; ++i)
{
int tx = tem.x + xd[i];
int ty = tem.y + yd[i];
int tz = tem.z + zd[i];
if (tx >= 0 && tx < L&&ty >= 0 && ty < M&&tz >= 0 && tz < N&&matrix[tx][ty][tz])
{
++volumn;
matrix[tx][ty][tz] = 0;
que.push(node(tx, ty, tz));
}
}
}
if (volumn >= T)
total += volumn;

}

int main()

{
scanf("%d%d%d%d", &M, &N, &L, &T);

int i, j, k;
for (k = 0; k < L; ++k)
for (j = 0; j < M; ++j)
for (i = 0; i < N; ++i)
scanf("%d", matrix[k][j] + i);

for (k = 0; k < L; ++k)
for (j = 0; j < M; ++j)
for (i = 0; i < N; ++i)
bfs(k, j, i);

printf("%d", total);
return 0;

}

/* 深度优先搜索----在数据规模一定情况下正确,过多会造成函数调用过多,堆栈溢出*/

#include <iostream>

using namespace std;

#define LMAX 60

#define MMAX 1280

#define NMAX 128

int matrix[LMAX][MMAX][NMAX];

int M, N, L, T, total = 0, volumn;

int xd[] = { 1,-1,0,0,0,0 };

int yd[] = { 0,0,1,-1,0,0 };

int zd[] = { 0,0,0,0,1,-1 };

void dfs(int x, int y, int z)

{
++volumn;
matrix[x][y][z] = 0;

int i;
for (i = 0; i < 6; ++i)
{
int tx = x + xd[i];
int ty = y + yd[i];
int tz = z + zd[i];
if (tx >= 0 && tx < L&&ty >= 0 && ty < M&&tz >= 0 && tz < N&&matrix[tx][ty][tz])
dfs(tx, ty, tz);
}

}

int main()

{
scanf("%d%d%d%d", &M, &N, &L, &T);

int i, j, k;
for (k = 0; k < L; ++k)
for (j = 0; j < M; ++j)
for (i = 0; i < N; ++i)
scanf("%d", matrix[k][j] + i);

for (k = 0; k < L; ++k)
for (j = 0; j < M; ++j)
for (i = 0; i < N; ++i)
if (matrix[k][j][i])
{
volumn = 0;
dfs(k, j, i);
if (volumn >= T)
total += volumn;
}

printf("%d", total);
return 0;

}


输出例子:

26



输出例子:

26
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息