Repository navigation
Expand file tree
/
Copy pathMain_14502.java
More file actions
113 lines (95 loc) · 3.06 KB
/
Copy pathMain_14502.java
File metadata and controls
113 lines (95 loc) · 3.06 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
package baekjoon;
import java.io.*;
import java.util.*;
public class Main_14502 {
static int n;
static int m;
static int[] dx = {-1, 0, 1, 0};
static int[] dy = {0, -1, 0, 1};
static int[][] board;
static int[][] visited;
static int answer = Integer.MIN_VALUE;
static class Pointer {
int x;
int y;
public Pointer(int x, int y) {
this.x = x;
this.y = y;
}
}
public static void getSafeZone(int[][] boardCopy) {
int safeZone = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (boardCopy[i][j] == 0) {
safeZone++;
}
}
}
answer = Math.max(safeZone, answer);
}
public static void dfs(int wall) {
if (wall == 3) {
Main_14502.bfs();
} else {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (board[i][j] == 0) {
board[i][j] = 1;
Main_14502.dfs(wall + 1);
board[i][j] = 0;
}
}
}
}
}
public static void bfs() {
Queue<Pointer> queue = new LinkedList<>();
// 벽 세우고 나서 바이러스 갯수 체크
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (board[i][j] == 2) {
queue.add(new Pointer(i, j));
}
}
}
/*
* 주의
* 2차원 배열 copy 시 원소안의 배열까지 clone 해야함
* */
int[][] boardCopy = board.clone();
for (int i = 0; i < n; i++) {
boardCopy[i] = board[i].clone();
}
// 벽이 세워진 상태에서 복사한 배열을 바이러스 전파
// 원본 배열 변경하면 최대값 못구함
while (!queue.isEmpty()) {
Pointer current = queue.poll();
for (int i = 0; i < dx.length; i++) {
int nx = current.x + dx[i];
int ny = current.y + dy[i];
if (nx >= 0 && ny >= 0 && nx < n && ny < m && boardCopy[nx][ny] == 0) {
queue.add(new Pointer(nx, ny));
boardCopy[nx][ny] = 2;
}
}
}
Main_14502.getSafeZone(boardCopy);
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
visited = new int[n][m];
board = new int[n][m];
for (int i = 0; i < n; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < m; j++) {
board[i][j] = Integer.parseInt(st.nextToken());
}
}
Main_14502.dfs(0);
System.out.println(answer);
}
}