-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
102 lines (88 loc) · 2.84 KB
/
Main.java
File metadata and controls
102 lines (88 loc) · 2.84 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class Main {
static int N, M, maxSafeZoneCnt=0;
static int[][] map, simulMap;
static List<Space> virusList;
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());
map = new int[N][M];
virusList = new ArrayList<>();
for(int i=0; i<N; i++){
st = new StringTokenizer(br.readLine());
for(int j=0; j<M; j++){
map[i][j] = Integer.parseInt(st.nextToken());
if(map[i][j] == 2){
virusList.add(new Space(i,j));
}
}
}
simulation(0,0);
System.out.println(maxSafeZoneCnt);
br.close();
}
// map을 완전탐색하면서 벽 3개일 경우 안전지대 최대 갯수 갱신
public static void simulation(int idx, int wall) {
if(wall == 3){
// copy current map to simulation map for testing spreading virus
simulMap = new int[N][M];
for(int i=0; i<N; i++){
for(int j=0; j<M; j++){
simulMap[i][j] = map[i][j];
}
}
// spread virus
for(Space virus : virusList){
dfs(virus.x, virus.y);
}
// get safe zone value
int safeZoneCnt = 0;
for(int i=0; i<N; i++){
for(int j=0; j<M; j++){
if(simulMap[i][j] == 0) safeZoneCnt++;
}
}
maxSafeZoneCnt = Math.max(maxSafeZoneCnt, safeZoneCnt);
return;
}
// back tracking 활용해서 벽을 3개 세웠을 모든 경우 탐색
for(int i=idx; i<N*M; i++){
int x = i / M;
int y = i % M;
if(map[x][y] == 0){
map[x][y] = 1;
simulation(i+1, wall+1);
map[x][y] = 0;
}
}
}
// dfs로 simulMap에 바이러스 퍼뜨려보기
public static void dfs(int x, int y){
int[] dx = {0,0,-1,1};
int[] dy = {-1,1,0,0};
for(int i=0; i<4; i++){
int nx = x + dx[i];
int ny = y + dy[i];
if(0<=nx && nx<N && 0<=ny && ny<M){
if(simulMap[nx][ny] == 0) {
simulMap[nx][ny] = 2;
dfs(nx,ny);
}
}
}
}
}
class Space {
int x, y;
public Space(int x, int y) {
this.x = x;
this.y = y;
}
}