forked from sameer2800/Lightoj-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1175-jane_and_frost_giants.cpp
176 lines (137 loc) · 3.35 KB
/
1175-jane_and_frost_giants.cpp
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <bits/stdc++.h>
#define ll long long
#define MAX 100100
using namespace std;
char arr[210][210];
char mark[210][210];
ll fdist[210][210];
ll jdist[210][210];
ll n, m;
void bfs2(int i, int j)
{
queue < pair <ll, pair <ll, ll > > > q;
q.push(make_pair(0, make_pair( i, j)));
memset(mark, -1, sizeof mark);
ll contr = 0;
while (!q.empty()) {
contr++;
//cout << "elem pos " << i << " " << j << " data " << jdist[i][j] << endl;
pair < ll, pair <ll, ll > > pr = q.front();
q.pop();
i = pr.second.first;
j = pr.second.second;
if (i < 0 || i >= n || j < 0 || j >= m )
continue;
if (mark[i][j] == 1)
continue;
jdist[i][j] = min(jdist[i][j], pr.first);
mark[i][j] = 1;
if ( arr[i - 1][j] == '.' ) {
q.push(make_pair(pr.first + 1, make_pair(i - 1, j) ));
}
if ( arr[i + 1][j] == '.') {
q.push(make_pair(pr.first + 1, make_pair(i + 1, j) ));
}
if ( arr[i][j - 1] == '.' ) {
q.push(make_pair(pr.first + 1, make_pair(i, j - 1) ));
}
if ( arr[i][j + 1] == '.' ) {
q.push(make_pair(pr.first + 1, make_pair(i, j + 1) ));
}
}
//cout << "contt " << contr << endl;
}
void bfs()
{
ll i, j;
queue < pair <ll, pair <ll, ll > > > q;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (arr[i][j] == 'F')
q.push(make_pair(0, make_pair( i, j)));
}
}
memset(mark, -1, sizeof mark);
ll contr = 0;
while (!q.empty()) {
contr++;
pair < ll, pair <ll, ll > >pr = q.front();
q.pop();
i = pr.second.first;
j = pr.second.second;
if (mark[i][j] == 1)
continue;
fdist[i][j] = min(fdist[i][j], pr.first);
mark[i][j] = 1;
if (i - 1 >= 0 ) {
if (i - 1 >= 0 and arr[i - 1][j] == '.' and mark[i - 1][j] == -1) {
q.push(make_pair(pr.first + 1, make_pair(i - 1, j) ));
}
}
if (i + 1 < n ) {
if (i + 1 < n and arr[i + 1][j] == '.' and mark[i + 1][j] == -1) {
q.push(make_pair(pr.first + 1, make_pair(i + 1, j) ));
}
}
if (j - 1 >= 0) {
if (j - 1 >= 0 and arr[i][j - 1] == '.' and mark[i][j - 1] == -1) {
q.push(make_pair(pr.first + 1, make_pair(i, j - 1) ));
}
}
if (j + 1 < m) {
if (j + 1 < m and arr[i][j + 1] == '.' and mark[i][j + 1] == -1) {
q.push(make_pair(pr.first + 1, make_pair(i, j + 1) ));
}
}
}
// cout << "contt " << contr << endl;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("/home/sameer/Documents/sameer/input.sam", "r", stdin);
#endif
ios_base::sync_with_stdio(false);
ll i, j, k, t, cont, a, b;
cin >> t;
ll cases = t;
while (t--) {
cin >> n >> m;
for (i = 0; i < n ; i++) {
for (j = 0; j < m; j++) {
fdist[i][j] = INT_MAX;
jdist[i][j] = INT_MAX;
}
}
for (i = 0; i < n ; i++) {
for (j = 0; j < m; j++) {
cin >> arr[i][j];
}
}
bfs();
for (i = 0; i < n ; i++) {
for (j = 0; j < m; j++) {
if (arr[i][j] == 'J')
bfs2(i, j);
}
}
ll temp = INT_MAX;
for (i = 0; i < m; i++) {
if (jdist[0][i] < fdist[0][i])
temp = min(temp, jdist[0][i]);
if (jdist[n - 1][i] < fdist[n - 1][i])
temp = min(temp, jdist[n - 1][i]);
}
for (i = 0; i < n; i++) {
if (jdist[i][0] < fdist[i][0])
temp = min(temp, jdist[i][0]);
if (jdist[i][m - 1] < fdist[i][m - 1])
temp = min(temp, jdist[i][m - 1]);
}
if (temp == INT_MAX)
cout << "Case " << cases - t << ": " << "IMPOSSIBLE" << endl;
else
cout << "Case " << cases - t << ": " << temp + 1 << endl;
}
return 0;
}