PAT甲级1091 Acute Stroke【三维bfs】

题目https://pintia.cn/problem-sets/994805342720868352/problems/994805375457411072

题意:

求三维的连通块

思路:

简单bfs

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<map>
 4 #include<set>
 5 #include<iostream>
 6 #include<cstring>
 7 #include<algorithm>
 8 #include<vector>
 9 #include<cmath> 
10 #include<stack>
11 #include<queue>
12 
13 #define inf 0x7fffffff
14 using namespace std;
15 typedef long long LL;
16 typedef pair<string, string> pr;
17 
18 int m, n, l, t;
19 struct node{
20     int x, y, z;
21     node(){
22     }
23     node(int _x, int _y, int _z){
24         x = _x;
25         y = _y;
26         z = _z;
27     }
28 };
29 
30 int dx[6] = {0, 0, 0, 0, 1, -1};
31 int dy[6] = {1, -1, 0, 0, 0, 0};
32 int dz[6] = {0, 0, 1, -1, 0, 0};
33 bool space[1300][130][65];
34 bool vis[1300][130][65];
35 int tot = 0;
36 
37 bool check(int i, int j, int k)
38 {
39     if(i < 0 || i >= m || j < 0 || j >= n || k < 0 || k >= l)return false;
40     else return true;
41 }
42 
43 void bfs(int x, int y, int z)
44 {
45     queue<node>que;
46     que.push(node(x, y, z));
47     vis[x][y][z] = true;
48     int cnt = 1;
49     while(!que.empty()){
50         node now = que.front();que.pop();
51         for(int i = 0; i < 6; i++){
52             int a = now.x + dx[i], b = now.y + dy[i], c = now.z + dz[i];
53             if(check(a, b, c) && !vis[a][b][c] && space[a][b][c]){
54                 que.push(node(a, b, c));
55                 vis[a][b][c] = true;
56                 cnt++;
57             }
58         }
59     }
60     if(cnt >= t){
61         tot += cnt;
62     }
63 }
64 
65 int main()
66 {
67     scanf("%d%d%d%d", &m, &n, &l, &t);
68     for(int k = 0; k < l; k++){
69         for(int i = 0; i < m; i++){
70             for(int j = 0; j < n; j++){
71                 scanf("%d", &space[i][j][k]);
72             }
73         }
74     }
75     
76     
77     for(int k = 0; k < l; k++){
78         for(int i = 0; i < m; i++){
79             for(int j = 0; j < n; j++){
80                 if(!vis[i][j][k] && space[i][j][k])
81                     bfs(i, j, k);
82             }
83         }
84     }
85     printf("%d\n", tot);
86     return 0;
87 }

猜你喜欢

转载自www.cnblogs.com/wyboooo/p/10658799.html
今日推荐