-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMain.java
More file actions
131 lines (113 loc) · 3.93 KB
/
Main.java
File metadata and controls
131 lines (113 loc) · 3.93 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
129
130
131
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
static int N;
static int M;
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static int[] dx = new int[]{0, 0, 1, -1};
static int[] dy = new int[]{1, -1, 0, 0};
static int[][] map;
static List<Virus> viruses;
static int answer = Integer.MAX_VALUE;
static class Virus{
int y;
int x;
public Virus(int y, int x) {
this.y = y;
this.x = x;
}
}
static class ActiveVirus extends Virus {
int cur;
public ActiveVirus(int y, int x, int cur){
super(y, x);
this.cur = cur;
}
public ActiveVirus(Virus virus, int cur){
super(virus.y, virus.x);
this.cur = cur;
}
}
public static void main(String[] args) throws IOException {
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
map = new int[N][N];
viruses = new ArrayList<>();
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){
viruses.add(new Virus(i, j));
}
}
}
searchOptimum(new ArrayList<>(), 0, M);
System.out.println(answer >= Integer.MAX_VALUE ? -1: answer);
}
public static void searchOptimum(List<Virus> current, int idx, int m){
if(current.size() == m){
answer = Math.min(answer, bfs(current));
} else {
for(int i = idx; i < viruses.size(); i ++){
current.add(viruses.get(i));
searchOptimum(current, i + 1, m);
current.remove(current.size() - 1);
}
}
}
public static int bfs(List<Virus> settingVirus){
int[][] visited = new int[N][N];
Queue<ActiveVirus> q = new LinkedList<>();
Boolean sure = true;
int res = 0;
for(int i = 0; i < N; i ++){
Arrays.fill(visited[i], -1);
}
for(Virus virus : settingVirus){
visited[virus.y][virus.x] = 0;
q.offer(new ActiveVirus(virus, 0));
}
while(!q.isEmpty()){
ActiveVirus now = q.poll();
for(int i = 0; i < 4; i++){
int ny = now.y + dy[i];
int nx = now.x + dx[i];
if(0 <= ny && ny < N && 0 <= nx && nx < N){
if(visited[ny][nx] == -1){
if(map[ny][nx] == 0) {
visited[ny][nx] = now.cur + 1;
q.offer(new ActiveVirus(ny, nx, now.cur + 1));
if(res >= now.cur + 1){
sure = true;
res = now.cur + 1;
}
res = Math.max(res, now.cur + 1);
sure = true;
} else if(map[ny][nx] == 2){
visited[ny][nx] = now.cur + 1;
q.offer(new ActiveVirus(ny, nx, now.cur + 1));
if(res > now.cur + 1){
sure = false;
res = now.cur + 1;
}
}
}
}
}
}
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
if (visited[i][j] == -1) {
if(map[i][j] == 0) {
return Integer.MAX_VALUE;
}
}
}
}
return sure ? res: res - 1;
}
}