50244. Connected Components in a Tower

難度:3.9/5

就三維dfs,然後is_valid寫好就會過了。

Used Time: 16:00

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

#define max(a,b) ((a)>(b)?)

int n;
int arr[80][80][80];
int vis[80][80][80] = {0};

int dx[6] = {1, -1, 0, 0, 0, 0};
int dy[6] = {0, 0, 1, -1, 0, 0};
int dz[6] = {0, 0, 0, 0, 1, -1};

static inline int is_valid(int x, int y, int z){
if(!(x >= 0 && x < n && y >= 0 && y < n && z >= 0 && z < n)) return 0;
return y <= x && z <= x;
}

int max_cnt = 0, max_num = -1;
int now_cnt = 0;
void dfs(int x, int y, int z){
vis[x][y][z] = 1;
now_cnt++;
for(int i = 0; i < 6; i++){
int now_x = x + dx[i], now_y = y + dy[i], now_z = z + dz[i];
if(!is_valid(now_x, now_y, now_z)) continue;
if(vis[now_x][now_y][now_z]) continue;
if(arr[x][y][z] != arr[now_x][now_y][now_z]) continue;
dfs(now_x, now_y, now_z);
}
}

int main(){
scanf("%d", &n);
for(int i = 0; i < n; i++){
for(int j = 0; j <= i; j++){
for(int k = 0; k <= i; k++){
scanf("%d", &arr[i][j][k]);
}
}
}

for(int i = 0; i < n; i++){
for(int j = 0; j <= i; j++){
for(int k = 0; k <= i; k++){
if(vis[i][j][k] == 0){
now_cnt = 0;
dfs(i, j, k);
if(now_cnt > max_cnt || (now_cnt == max_cnt && arr[i][j][k] > max_num)){
max_cnt = now_cnt, max_num = arr[i][j][k];
}
}
}
}
}

printf("%d %d\n", max_num, max_cnt);
}

50244. Connected Components in a Tower
https://aaronlin1229.github.io/judgegirl_50244/
Author
Akizumi
Posted on
July 17, 2023
Licensed under