1091 Acute Stroke (30point(s)) 需要二刷 *典型的BFS问题,和五子棋问题或许有关联
基本思想:
三维BFS遍历,注意可以XYZ变换数组进行快速遍历;
关键点:
注意访问的标志位问题;
#include<iostream> #include<stdlib.h> #include<stdio.h> #include<vector> #include<string> #include<math.h> #include<algorithm> #include<cstring> #include<map> #include<queue> #include<set> #include<stack> using namespace std; int m, n, l, t; int cnt = 0; struct node { int x, y, z; }; int matrix[1290][130][61]; bool flag[1290][130][61]; int X[6] = { 0,0,0,0,1,-1 }; int Y[6] = { 1,-1,0,0,0,0 }; int Z[6] = { 0,0,1,-1,0,0 }; void init() { fill(matrix[0][0], matrix[0][0] + 1290 * 130 * 61, 0); fill(flag[0][0], flag[0][0] + 1290 * 130 * 61, true); } bool charge(int x, int y, int z) { if (x >= m || x<0 || y>=n || y<0 || z>=l || z < 0) return false; if (!flag[x][y][z]||matrix[x][y][z]==0) return false; return true; } int bfs(int x,int y,int z) { int ct = 0; queue<node>q; node no; no.x = x; no.y = y; no.z = z; q.push(no); while (!q.empty()){ ct++; no = q.front(); q.pop(); flag[no.x][no.y][no.z] = false; //cout << no.x << " " << no.y << " " << no.z << endl; for (int i = 0; i < 6; i++) { int a = no.x + X[i]; int b = no.y + Y[i]; int c = no.z + Z[i]; if (charge(a,b,c)) { node noo; noo.x = a; noo.y = b; noo.z = c; q.push(noo); flag[a][b][c] = false; } } } if (ct >= t) return ct; else return 0; } int main() { init(); cin >> m >> n >> l >> t; for (int z = 0; z < l; z++) { for (int x = 0; x < m; x++) { for (int y = 0; y < n; y++) { scanf("%d", &matrix[x][y][z]); } } } for (int z = 0; z < l; z++) { for (int x = 0; x < m; x++) { for (int y = 0; y < n; y++) { if (flag[x][y][z]&&matrix[x][y][z]==1) cnt += bfs(x, y, z); } } } cout << cnt << endl; return 0; }

浙公网安备 33010602011771号