-
Notifications
You must be signed in to change notification settings - Fork 3
/
fire2.java
146 lines (135 loc) · 5.68 KB
/
fire2.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
import java.io.*;
import java.util.*;
public class fire2 {
private static char[][] map;
private static boolean[][] visited;
private static Queue<Node> queue;
private static Queue<Node> fires;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
int n = Integer.parseInt(in.readLine());
while (n-- > 0) {
StringTokenizer st = new StringTokenizer(in.readLine());
int c = Integer.parseInt(st.nextToken());
int r = Integer.parseInt(st.nextToken());
map = new char[r][c];
visited = new boolean[r][c];
queue = new LinkedList<>();
fires = new LinkedList<>();
for (int i = 0; i < r; i++) {
String line = in.readLine();
for (int j = 0; j < c; j++) {
char x = line.charAt(j);
if (x == '@') {
queue.add(new Node(i, j, 0));
} else if (x == '*') {
fires.add(new Node(i, j));
}
map[i][j] = x;
}
}
long count = Long.MAX_VALUE;
bfs: {
while (queue.size() != 0) {
// printMap(out);
// out.println();
int size = fires.size();
while(size -- > 0) {
Node fire = fires.poll();
if(validLoc(fire.r + 1, fire.c)) {
map[fire.r + 1][fire.c] = '*';
fires.add(new Node(fire.r + 1, fire.c));
}
if(validLoc(fire.r - 1, fire.c)) {
map[fire.r - 1][fire.c] = '*';
fires.add(new Node(fire.r - 1, fire.c));
}
if(validLoc(fire.r, fire.c + 1)) {
map[fire.r][fire.c + 1] = '*';
fires.add(new Node(fire.r, fire.c + 1));
}
if(validLoc(fire.r, fire.c - 1)) {
map[fire.r][fire.c - 1] = '*';
fires.add(new Node(fire.r, fire.c - 1));
}
}
size = queue.size();
while (size-- > 0) {
Node curr = queue.poll();
Node temp = null;
if(escaped(curr.r, curr.c)) {
temp = new Node(curr.r, curr.c, curr.count);
count = temp.count;
break bfs;
}
if (validLoc(curr.r + 1, curr.c) && !visited[curr.r + 1][curr.c]) {
visited[curr.r + 1][curr.c] = true;
temp = new Node(curr.r + 1, curr.c, curr.count + 1);
queue.add(temp);
if (escaped(curr.r + 1, curr.c)) {
count = temp.count;
break bfs;
}
}
if (validLoc(curr.r - 1, curr.c) && !visited[curr.r - 1][curr.c]) {
visited[curr.r - 1][curr.c] = true;
temp = new Node(curr.r - 1, curr.c, curr.count + 1);
queue.add(temp);
if (escaped(curr.r - 1, curr.c)) {
count = temp.count;
break bfs;
}
}
if (validLoc(curr.r, curr.c + 1) && !visited[curr.r][curr.c + 1]) {
visited[curr.r + 1][curr.c] = true;
temp = new Node(curr.r, curr.c + 1, curr.count + 1);
queue.add(temp);
if (escaped(curr.r, curr.c + 1)) {
count = temp.count;
break bfs;
}
}
if (validLoc(curr.r, curr.c - 1) && !visited[curr.r][curr.c - 1]) {
visited[curr.r][curr.c - 1] = true;
temp = new Node(curr.r, curr.c - 1, curr.count + 1);
queue.add(temp);
if (escaped(curr.r, curr.c - 1)) {
count = temp.count;
break bfs;
}
}
}
}
}
out.println(count == Long.MAX_VALUE ? "IMPOSSIBLE" : count + 1);
}
out.close();
}
private static boolean validLoc(int r, int c) {
return r >= 0 && r < map.length && c >= 0 && c < map[0].length
&& (map[r][c] != '#' && map[r][c] != '*');
}
private static boolean escaped(int r, int c) {
return r == 0 || r == map.length - 1 || c == 0 || c == map[0].length - 1;
}
private static void printMap(PrintWriter out) {
for(char[] arr : map)
out.println(arr);
}
}
class Node {
int r;
int c;
long count;
public Node(int r, int c, long count) {
this.r = r;
this.c = c;
this.count = count;
}
public Node(int r, int c) {
this.r = r;
this.c = c;
count = 0;
}
}