Skip to content

Commit fbb7c7a

Browse files
authored
[백준 20058] 마법사 상어와 파이어스톰 - 시뮬레이션
1 parent 106f738 commit fbb7c7a

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed

hoseok/week35/Boj20058.java

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
class Main {
5+
6+
private static final int[] rows = {-1, 0, 1, 0};
7+
private static final int[] cols = {0, 1, 0, -1};
8+
9+
private static int n, q, powN;
10+
private static int[][] map;
11+
12+
static class Node {
13+
int r, c;
14+
15+
Node(int r, int c) {
16+
this.r = r;
17+
this.c = c;
18+
}
19+
}
20+
21+
public static void main(String[] args) throws Exception {
22+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
23+
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
24+
StringTokenizer st = new StringTokenizer(br.readLine());
25+
n = Integer.parseInt(st.nextToken());
26+
powN = (int) Math.pow(2, n);
27+
q = Integer.parseInt(st.nextToken());
28+
map = new int[powN][powN];
29+
30+
for (int i = 0; i < powN; i++) {
31+
st = new StringTokenizer(br.readLine());
32+
for (int j = 0; j < powN; j++) {
33+
map[i][j] = Integer.parseInt(st.nextToken());
34+
}
35+
}
36+
37+
st = new StringTokenizer(br.readLine());
38+
while (q-- > 0) {
39+
int l = Integer.parseInt(st.nextToken());
40+
spinMap(l);
41+
decreaseIce();
42+
}
43+
44+
int totalIce = getTotalIce();
45+
int maxChunkSize = getMaxChunkSize();
46+
47+
bw.write(totalIce + "\n" + maxChunkSize);
48+
bw.flush();
49+
bw.close();
50+
}
51+
52+
public static int getMaxChunkSize() {
53+
int size = 0;
54+
boolean[][] visited = new boolean[powN][powN];
55+
for (int i = 0; i < powN; i++) {
56+
for (int j = 0; j < powN; j++) {
57+
if (!visited[i][j] && map[i][j] > 0) {
58+
size = Math.max(size, bfs(new Node(i, j), visited));
59+
}
60+
}
61+
}
62+
return size;
63+
}
64+
65+
public static int bfs(Node startNode, boolean[][] visited) {
66+
Queue<Node> que = new LinkedList<>();
67+
que.offer(startNode);
68+
visited[startNode.r][startNode.c] = true;
69+
int size = 1;
70+
71+
while (!que.isEmpty()) {
72+
Node curNode = que.poll();
73+
74+
for (int i = 0; i < 4; i++) {
75+
int nextR = curNode.r + rows[i];
76+
int nextC = curNode.c + cols[i];
77+
78+
if (nextR < 0 || nextR >= powN || nextC < 0 || nextC >= powN) {
79+
continue;
80+
}
81+
82+
if (!visited[nextR][nextC] && map[nextR][nextC] > 0) {
83+
size++;
84+
visited[nextR][nextC] = true;
85+
que.offer(new Node(nextR, nextC));
86+
}
87+
}
88+
}
89+
return size;
90+
}
91+
92+
public static int getTotalIce() {
93+
int sum = 0;
94+
for (int i = 0; i < powN; i++) {
95+
for (int j = 0; j < powN; j++) {
96+
sum += map[i][j];
97+
}
98+
}
99+
return sum;
100+
}
101+
102+
public static void decreaseIce() {
103+
int[][] newMap = new int[powN][powN];
104+
for (int i = 0; i < powN; i++) {
105+
for (int j = 0; j < powN; j++) {
106+
if (map[i][j] == 0) {
107+
continue;
108+
}
109+
int count = 0;
110+
for (int k = 0; k < 4; k++) {
111+
int nextR = i + rows[k];
112+
int nextC = j + cols[k];
113+
114+
if (nextR < 0 || nextR >= powN || nextC < 0 || nextC >= powN) {
115+
count++;
116+
continue;
117+
}
118+
119+
if (map[nextR][nextC] == 0) {
120+
count++;
121+
}
122+
}
123+
if (count > 1) {
124+
newMap[i][j] = Math.max(0, map[i][j] - 1);
125+
} else {
126+
newMap[i][j] = map[i][j];
127+
}
128+
}
129+
}
130+
map = newMap;
131+
}
132+
133+
public static void spinMap(int l) {
134+
int[][] copyMap = new int[powN][powN];
135+
136+
int targetLength = (int) Math.pow(2, l);
137+
for (int i = 0; i < powN; i += targetLength) {
138+
for (int j = 0; j < powN; j += targetLength) {
139+
int mapRow = i;
140+
int mapCol = j;
141+
for (int k = j; k < j + targetLength; k++) {
142+
for (int h = i + targetLength - 1; h >= i; h--) {
143+
copyMap[mapRow][mapCol++] = map[h][k];
144+
}
145+
mapCol = j;
146+
mapRow++;
147+
}
148+
}
149+
}
150+
map = copyMap;
151+
}
152+
}

0 commit comments

Comments
 (0)