-
Notifications
You must be signed in to change notification settings - Fork 0
/
5427.java
121 lines (92 loc) · 2.31 KB
/
5427.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
import java.io.*;
import java.util.*;
public class Main {
static int R, C, ans;
static char[][] map;
static Queue<int[]> fire = new LinkedList<>();
static Queue<int[]> player = new LinkedList<>();
static boolean[][] visit;
static int dx[] = {0,0,1,-1};
static int dy[] = {1,-1,0,0};
public static void main(String[] args) throws Exception{
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int T = Integer.parseInt(input.readLine());
while(T-- > 0) {
String rc[] = input.readLine().split(" ");
R = Integer.parseInt(rc[1]);
C = Integer.parseInt(rc[0]);
map = new char[R][C];
visit = new boolean[R][C];
ans = 0;
int start[] = new int[2];
for(int i=0; i<R; i++) {
char row[] = input.readLine().toCharArray();
for(int j=0; j<C; j++) {
map[i][j] = row[j];
if(row[j] == '@') {
start[0] = i;
start[1] = j;
map[i][j] = '.';
visit[i][j] = true;
player.add(new int[] {i,j});
}else if(row[j] == '*') {
fire.add(new int[] {i, j});
}
}
}
int ans = escape();
sb.append(ans > 0 ? ans : "IMPOSSIBLE");
sb.append("\n");
fire.clear();
player.clear();
}
System.out.println(sb);
}
static int escape() {
int time = 0;
while(!player.isEmpty()) {
time++;
if(!fire.isEmpty()) {
spreadFire();
}
if(run()) return time;
}
return -1;
}
static boolean run() {
int size = player.size();
while(size-- > 0) {
int cur[] = player.poll();
if(cur[0] == 0 || cur[0] == R-1 || cur[1] == 0 || cur[1] == C-1) {
return true;
}
for(int i=0; i<4; i++) {
int nx = cur[0]+dx[i];
int ny = cur[1]+dy[i];
if(rangeCheck(nx, ny) && !visit[nx][ny]) {
visit[nx][ny] = true;
player.add(new int[] {nx, ny});
}
}
}
return false;
}
static void spreadFire() {
int size = fire.size();
while(size-- > 0) {
int cur[] = fire.poll();
for(int i=0; i<4; i++) {
int nx = cur[0]+dx[i];
int ny = cur[1]+dy[i];
if(rangeCheck(nx, ny)) {
map[nx][ny] = '*';
fire.add(new int[] {nx, ny});
}
}
}
}
static boolean rangeCheck(int x, int y) {
return x>-1 && y>-1 && x<R && y<C && map[x][y] == '.' ? true : false;
}
}