-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain17142_연구소3.java
117 lines (104 loc) · 2.56 KB
/
Main17142_연구소3.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
109
110
111
112
113
114
115
116
117
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.StringTokenizer;
//2019-08-08
public class Main17142_연구소3 {
private static int N, M, ans;
private static int empty;
private static int[][] map;
private static List<pair> space;
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()); // 활성화할 바이러스
space = new ArrayList<>();
map = new int[N][N];
empty = 0;
// 0은 빈칸, 1은 벽, 2는 바이러스위치
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < N; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
if (map[i][j] == 2) {
space.add(new pair(i, j));
} else if (map[i][j] == 0) {
empty++;
}
}
}
if (empty == 0) {
System.out.println("0");
return;
}
set = new pair[M];
ans = Integer.MAX_VALUE;
chooseSpace(0, 0);
if (ans == Integer.MAX_VALUE)
ans = -1;
System.out.println(ans);
}// end of main
private static pair[] set;
public static void chooseSpace(int len, int k) {
// list에서 m개를 골라라 .
if (len == M) {
int ret = activateVirus();
if (ret > 0) {
ans = ans > ret ? ret : ans;
}
return;
}
for (int i = k; i < space.size(); i++) {
set[len] = space.get(i);
chooseSpace(len + 1, i + 1);
}
}
static int[] dy = { -1, 1, 0, 0 };
static int[] dx = { 0, 0, -1, 1 };
public static int activateVirus() {
int remain = empty;
Queue<pair> que = new LinkedList<>();
boolean[][] visit = new boolean[N][N];
for (pair p : set) {
que.add(p);
visit[p.y][p.x] = true;
}
int time = 0;
while (!que.isEmpty()) {
time++;
int qSize = que.size();
for (int q = 0; q < qSize; q++) {
pair poll = que.poll();
for (int d = 0; d < 4; d++) {
int ny = poll.y + dy[d];
int nx = poll.x + dx[d];
if (ny < 0 || ny > N - 1 || nx < 0 || nx > N - 1 || visit[ny][nx] || map[ny][nx] == 1) {
continue;
}
if (map[ny][nx] == 0) {// 빈칸인 경우
remain--;
if (remain == 0) {
return time;
}
}
que.add(new pair(ny, nx));
visit[ny][nx] = true;
}
}
}
return 0;
}
static class pair {
int y;
int x;
public pair(int y, int x) {
super();
this.y = y;
this.x = x;
}
}
}