PAT甲級 1091 Acute Stroke (30 分) (bfs)

題目鏈接:傳送門

思路:題目是找出大小大於t的1的聯通塊,然後求這些塊大小的和。
所以直接bfs模擬即可。(讀題讀了好久。。)

代碼:

#include <bits/stdc++.h>

using namespace std;

const int maxn = 2048 , N = 100 , M = 200;

struct node {
	int x , y , d;
	node(int a , int b , int c):x(a) , y(b) , d(c){}
};


bool g[N][maxn][M];
bool vis[N][maxn][M];
int dx[] = {0 , 0 , 1 , -1 , 0 , 0};
int dy[] = {1 , -1 , 0 , 0 , 0 , 0};
int dz[] = {0 , 0 , 0 , 0 , 1 , -1};



int main() {
	int m , n , l , t;
	cin >> m >> n >> l >> t;
	for(int k = 1 ; k <= l ; k++) {
		for(int i = 1 ; i <= m ; i++) {
			for(int j = 1 ; j <= n; j++) {
				cin >> g[k][i][j];
			}
		}
	}
	queue<node> q;
	int ans = 0;
	for(int i = 1 ; i <= l ; i++) {
		for(int j = 1 ; j <= m ; j ++) {
			for(int k = 1 ; k <= n ; k++) {
				if(!vis[i][j][k] && g[i][j][k]) {
					q.push(node(j , k , i));
					vis[i][j][k] = 1;
					int cnt = 0;
					while(!q.empty()) {
						node u = q.front(); q.pop();
						cnt++;
						for(int t1 = 0 ; t1 < 6 ; t1++) {
							int x = u.x + dx[t1] , y = u.y + dy[t1] , d = u.d + dz[t1];
							if(x >= 1 && x <= m && y >= 1 && y <= n && d >= 1 && d <= l && g[d][x][y] && !vis[d][x][y]) {
								q.push(node(x , y , d));
								vis[d][x][y] = 1;
							}
						}
					}
					if(cnt >= t)ans += cnt;
				}
			}
		}
	}
	cout << ans << "\n";
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章