-
Notifications
You must be signed in to change notification settings - Fork 0
/
연구소.java
108 lines (82 loc) · 1.96 KB
/
연구소.java
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
import java.util.*;
import java.io.*;
public class Main {
static int arr[][];
static int dx[] = {-1,1 ,0,0};
static int dy[] = {0,0 ,-1,1};
static int N,M;
static int maxSafeZone = Integer.MIN_VALUE;
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());
M = Integer.parseInt(st.nextToken());
arr = new int[N][M];
for(int i =0; i<N; i++) {
st = new StringTokenizer(br.readLine());
for(int j = 0; j<M; j ++) {
arr[i][j] = Integer.parseInt(st.nextToken());
}
}
dfs(0);
System.out.println(maxSafeZone);
}
static void dfs(int wallCnt) {
if(wallCnt == 3) {
bfs();
return;
}
for(int i =0; i<N; i++) {
for(int j =0; j<M; j++ ){
if(arr[i][j] == 0) {
arr[i][j] = 1;
dfs(wallCnt+1);
arr[i][j] = 0;
}
}
}
}
static void bfs() {
Queue<int []> queue = new LinkedList<>();
for(int i=0; i<N; i++) {
for(int j = 0; j<M; j++) {
if(arr[i][j] == 2) {
queue.add(new int[] { i,j});
}
}
}
int copyArr[][] = new int[N][M];
for(int i =0; i<N; i++) {
for(int j = 0; j<M; j++) {
copyArr[i][j] = arr[i][j];
}
}
while(!queue.isEmpty()) {
int [] now = queue.poll();
int x = now[0];
int y = now[1];
for(int k = 0; k<4; k++) {
int nx = x+ dx[k];
int ny = y+ dy[k];
if(nx >=0 && nx<N && ny>=0 && ny <M) {
if(copyArr[nx][ny] == 0) {
queue.add(new int[]{ nx,ny});
copyArr[nx][ny] = 2;
}
}
}
}
curSafeZone(copyArr);
}
static void curSafeZone(int[][] copyArr) {
int safeZone =0;
for(int i =0; i<N; i++) {
for(int j = 0; j<M; j++) {
if(copyArr[i][j] ==0)
safeZone++;
}
}
if(maxSafeZone < safeZone)
maxSafeZone= safeZone;
}
}