-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path18809_Gaaaaaaaaaarden.java
More file actions
128 lines (118 loc) ยท 4.94 KB
/
18809_Gaaaaaaaaaarden.java
File metadata and controls
128 lines (118 loc) ยท 4.94 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
116
117
118
119
120
121
122
123
124
125
126
127
128
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
static int N, M, G, R, ans; //์
๋ ฅ๊ฐ ๋ฐ ๊ฒฐ๊ณผ๊ฐ
static int map[][], rArr[], gArr[]; //์
๋ ฅ๋ฐฐ์ด, red๋ฐฐ์์ก ๋ฐฐ์ด, green๋ฐฐ์์ก ๋ฐฐ์ด
static boolean[] v; //์กฐํฉ์์ ์ฌ์ฉํ ๋ฐฉ๋ฌธ๋ฐฐ์ด
static int[] dx = new int[] {-1,1,0,0}; //๋ฐฉํฅ๋ฒกํฐ
static int[] dy = new int[] {0,0,-1,1}; //๋ฐฉํฅ๋ฒกํฐ
static ArrayList<int[]> list; //๋ฐฐ์์ก์ ์ฌ์ ์ ์๋ ์ขํ๋ค์ ๊ด๋ฆฌํ๋ ๋ฆฌ์คํธ
static Queue<int[]> queue = new ArrayDeque<>(); //BFS์์ ์ฌ์ฉํ ์ ์ญ ํ
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());
G = Integer.parseInt(st.nextToken());
R = Integer.parseInt(st.nextToken());
map = new int[N][M];
list = new ArrayList<>();
for (int i = 0; i < N; i++) { //๋งต ์
๋ ฅ
st = new StringTokenizer(br.readLine());
for (int j = 0; j < M; j++) {
int idx = Integer.parseInt(st.nextToken());
map[i][j] = idx;
if (idx == 2) list.add(new int[]{i, j});
}
}
v = new boolean[list.size()]; //list๊ธธ์ด์์ ์กฐํฉ์ ์ฌ์ฉ
rArr = new int[R];
gArr = new int[G];
greenPermu(0, 0);
System.out.println(ans);
}
private static void greenPermu(int start, int cnt) { //green๋ฐฐ์์ก์ ์ฌ์ ์์น๋ฅผ ๋ฝ๊ธฐ์ํ ์กฐํฉ
if(cnt == G) {
//์์ด ์์ฑ์๋ฃ
redPermu(0, 0); //green์ ๋ค ๋ฝ์์ผ๋ฉด red๋ฅผ ์ ํํ๋ฌ
} else {
for(int i=start; i<list.size(); i++) {
if(v[i]) continue; //๋ฐฉ๋ฌธ๋ฐฐ์ด์ ์ฌ์ฉํ ์ด์ ๋ ์ ํ๋ green์๋ฆฌ๋ฅผ ์ ์ธํ ๋๋จธ์ง ์๋ฆฌ์์ red์๋ฆฌ๋ฅผ ์ ํํด์ผํ๊ธฐ ๋๋ฌธ
v[i] = true;
gArr[cnt] = i;
greenPermu(i + 1,cnt + 1);
v[i] = false;
}
}
}
private static void redPermu(int start, int cnt) { //red๋ฐฐ์์ก์ ์ฌ์ ์์น๋ฅผ ๋ฝ๊ธฐ์ํ ์กฐํฉ
if(cnt == R) {
//์์ด ์์ฑ์๋ฃ
bfs(); //๋ฐฐ์์ก ํผํธ๋ฆฌ๊ธฐ
} else {
for(int i=start; i<list.size(); i++) {
if(v[i]) continue;
v[i] = true;
rArr[cnt] = i;
redPermu(i + 1, cnt + 1);
v[i] = false;
}
}
}
private static void bfs() { //๋ฐฐ์์ก์ ํผํธ๋ฆฌ๋ฉฐ ๋์ ํ์ธ
//์์ ๋งต ๊น์ ๋ณต์ฌ
Point[][] temp = new Point[N][M];
for(int i=0; i<N; i++) {
for(int j=0; j<M; j++) {
temp[i][j] = new Point(map[i][j], 0);
}
}
//๊ทธ๋ฆฐ๋ถํฐ ์ฌ์ผ๋ฉด์ ํ์ ์ถ๊ฐ
for(int i=0; i<gArr.length; i++) {
int[] idx = list.get(gArr[i]);
queue.offer(new int[]{idx[0], idx[1]});
temp[idx[0]][idx[1]] = new Point(3, 0);
}
//๋ ๋
for(int i=0; i<rArr.length; i++) {
int[] idx = list.get(rArr[i]);
queue.offer(new int[]{idx[0], idx[1]});
temp[idx[0]][idx[1]] = new Point(4, 0);
}
int sum = 0; //๊ฝ์ ๊ฐ์
while(!queue.isEmpty()) {
int[] idx = queue.poll();
int time = temp[idx[0]][idx[1]].time; //ํ์ฌ์์น ์๊ฐ
int type = temp[idx[0]][idx[1]].type; //ํ์ฌ์์น ์ํ
if(temp[idx[0]][idx[1]].type == -1) continue; //๊ฝ์ด ๋ ์๋ฆฌ๊ฐ ํ์ ์์์ ๊ฒฝ์ฐ
for(int i=0; i<4; i++) {
int nx = idx[0] + dx[i];
int ny = idx[1] + dy[i];
if(nx<0 || ny<0 || nx>N-1 || ny>M-1) continue; //์์ธ์ฒ๋ฆฌ
if(temp[nx][ny].type == 1 || temp[nx][ny].type == 2) { //ํ์ฅ ๊ฐ๋ฅ
temp[nx][ny].type = type;
temp[nx][ny].time = time + 1;
queue.offer(new int[]{nx, ny});
continue;
}
if(temp[nx][ny].type != type && temp[nx][ny].type != -1) { //๋ค๋ฅธ ๋ฐฐ์์ก์ด ์กด์ฌํ ๊ฒฝ์ฐ
if((temp[idx[0]][idx[1]].time + 1) == temp[nx][ny].time) { //๋์๊ฐ๋ ๊ฒน์น๋ ๊ฒฝ์ฐ
temp[nx][ny].type = -1; //๊ฝ ์์ฑ
sum++;
}
}
}
}
ans = Math.max(ans, sum); //๊ฒฐ๊ณผ๊ฐ ๋น๊ต ๋ฐ ๊ฐฑ์
}
static class Point { //๋งต ์ขํ์ ์ ๋ณด ํด๋์ค -> ์์๋ฐฐ์ด์์ ์ฌ์ฉ
int type;
int time;
Point(int type, int time) {
this.type = type;
this.time = time;
}
}
}