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×N matrix, and the maximum resolution is 1286 by 128); L (≤) 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
本题呢是一道bfs的裸题,题意是给定一个图,让你计算出图中所有连通域中连通块个数不少于指定数目
的连通块的总块数。可能有多个连通域。
直接bfs。
bfs,把和一个状态有关的状态加进去,加一些特殊判断条件,满足情况的留下,对剩余的重复之前的步骤,直到无路可走。
dfs会栈溢出哈!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1 #include <cstdio> 2 #include <cstring> 3 #include <queue> 4 #include <cmath> 5 using namespace std; 6 7 int maze[65][1300][130]; 8 bool vis[65][1300][130]; 9 10 int n, m, piece, limit; 11 12 int ans, num, cnt; 13 14 struct B { 15 int p, x, y; 16 }; 17 18 void bfs(int p, int x, int y) { 19 queue <B> que; 20 que.push(B{p, x, y}); 21 while(!que.empty()) { 22 B u = que.front(); 23 que.pop(); 24 if(u.p < 0 || u.p >= piece || u.x < 0 || u.x >= n || u.y < 0 || u.y >= m || vis[u.p][u.x][u.y] || !maze[u.p][u.x][u.y]) { 25 continue; 26 } 27 num ++; 28 vis[u.p][u.x][u.y] = true; 29 for(int dx = -1; dx <= 1; dx ++) { 30 for(int dy = -1; dy <= 1; dy ++) { 31 if(abs(dx - dy) == 1) { 32 que.push(B{u.p, u.x + dx, u.y + dy}); 33 } 34 } 35 } 36 for(int dp = -1; dp <= 1; dp ++) { 37 if(dp != 0) { 38 que.push(B{u.p + dp, u.x, u.y}); 39 } 40 } 41 } 42 } 43 44 int main() { 45 scanf("%d %d %d %d", &n, &m, &piece, &limit); 46 for(int i = 0; i < piece; i ++) { 47 for(int j = 0; j < n; j ++) { 48 for(int k = 0; k < m; k ++) { 49 scanf("%d", &maze[i][j][k]); 50 } 51 } 52 } 53 for(int i = 0; i < piece; i ++) { 54 for(int j = 0; j < n; j ++) { 55 for(int k = 0; k < m; k ++) { 56 if(!vis[i][j][k] && maze[i][j][k]) { 57 num = 0; 58 bfs(i, j, k); 59 if(num >= limit) ans += num; 60 } 61 } 62 } 63 } 64 printf("%d ", ans); 65 return 0; 66 }