您的位置:首页 > 其它

PAT 1091. Acute Stroke (30)(孤岛问题)

2016-10-10 17:05 405 查看

官网

题目

1091. Acute Stroke (30)

时间限制

400 ms

内存限制

65536 kB

代码长度限制

16000 B

判题程序

Standard

作者

CHEN, Yue

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

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

题目大意

1.第一行M,N,L,T,M,N代表每一片的大小,最高是1286×128,L<=60是切片的数量,T是阈值如果connected core的volume小于T,则不用计算。

2.接下来给出L个切片,每个切片的1代表stroke,0代表正常,这里有几个分离的core regions,只计算他们volumes值不小于T的。

3.输出中stroke core 的个数,感觉就是求孤岛的个数,图差评。

解题思路

1.还是孤岛问题,只是换成三维了。

AC代码

#include<iostream>
#include<deque>
#include<fstream>

using namespace std;
//fstream in("1.txt");
//#define cin in
int m, n, l, t;
int slices[1287][130][61], visited[1287][130][61];
int s, sum = 0;
struct node{
int x, y, z;
node(){}
node(int _x, int _y, int _z){
this->x = _x;
this->y = _y;
this->z = _z;
}
};
void bfs(int r, int c, int lever)
{
deque<node> dq;
dq.push_back(node(r, c, lever));
while (!dq.empty())
{
node now;
now = dq.front();
visited[now.x][now.y][now.z] = 1;
dq.pop_front();
//上下左右前后
if (now.z + 1 < l && slices[now.x][now.y][now.z + 1] == 1 && visited[now.x][now.y][now.z + 1]==0)
{
dq.push_back(node(now.x, now.y, now.z + 1));
visited[now.x][now.y][now.z + 1] = 1;
s++;
}
if (now.z - 1 >= 0 && slices[now.x][now.y][now.z - 1] == 1 && visited[now.x][now.y][now.z - 1] == 0)
{
dq.push_back(node(now.x, now.y, now.z - 1));
visited[now.x][now.y][now.z - 1] = 1;
s++;
}
if (now.x + 1 < m && slices[now.x + 1][now.y][now.z] == 1 && visited[now.x + 1][now.y][now.z] == 0)
{
dq.push_back(node(now.x + 1, now.y, now.z));
visited[now.x + 1][now.y][now.z] = 1;
s++;
}
if (now.x - 1 >= 0 && slices[now.x - 1][now.y][now.z] == 1 && visited[now.x - 1][now.y][now.z] == 0)
{
dq.push_back(node(now.x - 1, now.y, now.z));
visited[now.x - 1][now.y][now.z] = 1;
s++;
}
if (now.y + 1 < n && slices[now.x][now.y + 1][now.z] == 1 && visited[now.x][now.y + 1][now.z] == 0)
{
dq.push_back(node(now.x, now.y + 1, now.z));
visited[now.x][now.y + 1][now.z] = 1;
s++;
}
if (now.y - 1 >= 0 && slices[now.x][now.y - 1][now.z] == 1 && visited[now.x][now.y - 1][now.z] == 0)
{
dq.push_back(node(now.x, now.y - 1, now.z));
visited[now.x][now.y - 1][now.z] = 1;
s++;
}

}
}
int main()
{
cin >> 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++)
{
cin >> slices[j][k][i];
}
}
}
for (int i = 0; i < l; i++)
{
;
for (int j = 0; j < m; j++)
{
;
for (int k = 0; k < n; k++)
{
//bfs搜索是孤岛及其成员的个数
if (visited[j][k][i]==0&&slices[j][k][i]==1)
{
s = 1;
bfs(j, k, i);
}
//如果成员个数大于要求,则记入总数
if (s>=t)
{
sum += s;;
s = 0;
}
}
}
}
cout << sum << endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: