-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14502.java
More file actions
115 lines (99 loc) · 2.71 KB
/
14502.java
File metadata and controls
115 lines (99 loc) · 2.71 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
// 백준 14502번
// bfs
// 벽세우는것을 브루트 포스로 다 해봐야함
import java.io.*;
import java.util.*;
public class Main {
static int M,N;
static int[][] lab;
static int[][] wall;
static int[] dx = {1, 0, -1, 0};
static int[] dy = {0, 1, 0, -1};
static int result = Integer.MIN_VALUE;
public static int[][] copy(int[][] arr){
int[][] n = new int[N][M];
for(int i=0;i<N;i++){
for(int j =0;j<M;j++){
n[i][j]=arr[i][j];
}
}
return n;
}
public static void makeWall(int depth) {
if (depth == 3) {
bfs();
return;
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (wall[i][j] == 0) {
wall[i][j] = 1;
makeWall(depth + 1);
wall[i][j] = 0;
}
}
}
}
public static int toInt(String s){
return Integer.parseInt(s);
}
static void bfs(){
int[][] newWall = copy(wall);
Queue<Point> q = new LinkedList<>();
for(int i=0;i<N;i++){
for(int j =0;j<M;j++){
if(newWall[i][j]==2){
q.add(new Point(i,j));
}
}
}
while(!q.isEmpty()){
int r = q.peek().x;
int c = q.peek().y;
q.poll();
for(int i=0;i<4;i++){
int nr = r + dx[i];
int nc = c + dy[i];
if ((nr>=0&&nc>=0&&nr<N&&nc<M) && newWall[nr][nc] ==0){
newWall[nr][nc] = 2;
q.add(new Point(nr,nc));
}
}
}
int cnt = 0;
for(int i=0;i<N;i++){
for(int j =0;j<M;j++){
if(newWall[i][j]==0){
cnt++;
}
}
}
result = Math.max(result,cnt);
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = toInt(st.nextToken());
M = toInt(st.nextToken());
lab = new int[N][M];
for (int i=0;i<N;i++){
st = new StringTokenizer(br.readLine());
int j=0;
while (st.hasMoreTokens()){
lab[i][j] = Integer.parseInt(st.nextToken());
j++;
}
}
wall = copy(lab);
makeWall(0);
System.out.println(result);
}
}
class Point {
int x;
int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}