-
Notifications
You must be signed in to change notification settings - Fork 2
/
김지원.java
149 lines (109 loc) · 2.97 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main {
private static int n, m;
private static int[][] map;
private static boolean[] visit;
private static int[] dx = {-1, 0, 1, 0};
private static int[] dy = {0, 1, 0, -1};
private static int result = Integer.MAX_VALUE;
private static int val;
private static ArrayList<Virus> virusList = new ArrayList();
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(reader.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
map = new int[n+1][n+1];
for (int i=1; i<=n; i++) {
st = new StringTokenizer(reader.readLine());
for (int j=1; j<=n; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
if (map[i][j] == 2) {
virusList.add(new Virus(i, j));
map[i][j] = 0;
}
}
}
visit = new boolean[virusList.size()];
dfs(0,0);
if (result == Integer.MAX_VALUE) {
System.out.println(-1);
} else {
System.out.println(result-1);
}
}//main
private static void dfs(int start, int count) {
if (count >= m) {
//바이러스 퍼뜨리기
spreadVirus();
//최소시간 비교
result = Math.min(result, val);
} else {
for (int i=start; i<virusList.size(); i++) {
if (!visit[i]) {
visit[i] = true;
dfs(start+1, count+1);
visit[i] = false;
}
}
}
}
private static void spreadVirus() {
Queue<Virus> q = new LinkedList();
int[][] temp = new int[n+1][n+1];
int[][] time = new int[n+1][n+1];
for (int i=1; i<=n; i++) {
temp[i] = map[i].clone();
}
//방문처리 한 바이러스에 진짜 바이러스
for (int i=0; i<virusList.size(); i++) {
if (visit[i]) {
map[virusList.get(i).x][virusList.get(i).y] = 0;
time[virusList.get(i).x][virusList.get(i).y] = 1;
q.add(virusList.get(i));
}
}
val = 0;
while (!q.isEmpty()) {
Virus virus = q.remove();
int qx = virus.x;
int qy = virus.y;
int qt = time[qx][qy];
val = Math.max(val, qt);
for (int i=0; i<dx.length; i++) {
int nx = qx + dx[i];
int ny = qy + dy[i];
if (nx < 1 || ny < 1 || nx > n || ny > n || temp[nx][ny] != 0 || time[nx][ny] != 0) {
continue;
}
//temp가 0이상이고, time이 0일 때
if (temp[nx][ny] == 0 && time[nx][ny] == 0) {
time[nx][ny] = qt + 1;
temp[nx][ny] = 2;
q.add(new Virus(nx, ny));
}
}
}//while
for (int i=1; i<=n; i++) {
for (int j=1; j<=n; j++) {
if (temp[i][j] == 0 && time[i][j] == 0) {
val = Integer.MAX_VALUE;
return;
}
}
}
}
static class Virus {
int x, y;
public Virus(int x, int y) {
this.x = x;
this.y = y;
}
}
}