-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJava_20058.java
More file actions
131 lines (117 loc) · 3.98 KB
/
Java_20058.java
File metadata and controls
131 lines (117 loc) · 3.98 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
public class Java_20058 {
static int N;
static int[][] map;
static int[] dx = {-1,0, 1, 0};
static int[] dy = { 0,1, 0, -1};
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
int Q = Integer.parseInt(st.nextToken());
N = (int) Math.pow(2, N);
map = new int[N][N];
int[] ls = new int[Q];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < N; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
}
}
st = new StringTokenizer(br.readLine());
for (int i = 0; i < Q; i++) {
ls[i] = Integer.parseInt(st.nextToken());
}
for (int i = 0; i < Q; i++) {
fireStorm((int)Math.pow(2,ls[i]));
melt();
}
System.out.println(countIce());
System.out.println(countMass());
}
public static void fireStorm(int length) {
int[][] copyMap = new int[N][N];
for (int i = 0; i < N; i ++) {
for (int j = 0; j < N; j ++) {
copyMap[i][j]=map[i][j];
}
}
for (int i = 0; i < N; i += length) {
for (int j = 0; j < N; j += length) {
//rotate
for (int a = 0; a < length; a ++) {
for (int b = 0; b < length; b ++) {
int ta = b;
int tb = (length-1)-a;
map[i+ta][j+tb]=copyMap[i+a][j+b];
}
}
}
}
}
public static void melt() {
int[][] copyMap = new int[N][N];
for (int i = 0; i < N; i ++) {
for (int j = 0; j < N; j ++) {
copyMap[i][j]=map[i][j];
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
int cnt = 0;
if(map[i][j]==0) continue;
for (int d = 0; d < 4; d++) {
int tx = i + dx[d];
int ty = j + dy[d];
if (isValidate(tx, ty) && copyMap[tx][ty] > 0) {
cnt++;
}
}
if (cnt < 3) map[i][j]--;
}
}
}
public static int countIce() {
int cnt = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cnt += map[i][j];
}
}
return cnt;
}
public static int countMass() {
ArrayList<Integer> massList = new ArrayList<>();
massList.add(0);
boolean[][] isVisited = new boolean[N][N];
Queue<int[]> que = new LinkedList<>();
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (isVisited[i][j]) continue;
isVisited[i][j] = true;
if (map[i][j] <= 0) continue;
int cnt = 1;
que.add(new int[]{i, j});
while (!que.isEmpty()) {
int[] current = que.poll();
for (int d = 0; d < 4; d++) {
int tx = current[0] + dx[d];
int ty = current[1] + dy[d];
if (isValidate(tx, ty) && !isVisited[tx][ty] && map[tx][ty] > 0) {
que.add(new int[]{tx, ty});
cnt++;
isVisited[tx][ty]=true;
}
}
}
massList.add(cnt);
}
}
return Collections.max(massList);
}
static boolean isValidate(int x, int y) {
return (x >= 0 && x < N && y >= 0 && y < N);
}
}