#include <bits/stdc++.h>
using namespace std;
// macros
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<ll, ll> lll;
typedef tuple<int, int, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<ll> vll;
typedef vector<lll> vlll;
#define REP(a,b,c) for(int a=int(b); a<int(c); a++)
#define RE(a,c) REP(a,0,c)
#define RE1(a,c) REP(a,1,c+1)
#define REI(a,b,c) REP(a,b,c+1)
#define REV(a,b,c) for(int a=int(c-1); a>=int(b); a--)
#define FOR(a,b) for(auto& a : b)
#define all(a) a.begin(), a.end()
#define INF 1e18
#define EPS 1e-9
#define pb push_back
#define popb pop_back
#define fi first
#define se second
// input
template<class T> void IN(T& x) {cin >> x;}
template<class H, class... T> void IN(H& h, T&... t) {IN(h); IN(t...); }
// output
template<class T1, class T2> void OUT(const pair<T1,T2>& x);
template<class T> void OUT(const vector<T>& x);
template<class T> void OUT(const T& x) {cout << x;}
template<class H, class... T> void OUT(const H& h, const T&... t) {OUT(h); OUT(t...); }
template<class T1, class T2> void OUT(const pair<T1,T2>& x) {OUT(x.fi,' ',x.se);}
template<class T> void OUT(const vector<T>& x) {RE(i,x.size()) OUT(i==0?"":" ",x[i]);}
template<class... T> void OUTL(const T&... t) {OUT(t..., "\n"); }
template<class H> void OUTLS(const H& h) {OUTL(h); }
template<class H, class... T> void OUTLS(const H& h, const T&... t) {OUT(h,' '); OUTLS(t...); }
// dp
template<class T> bool ckmin(T&a, T&b) { bool bl = a > b; a = min(a,b); return bl;}
template<class T> bool ckmax(T&a, T&b) { bool bl = a < b; a = max(a,b); return bl;}
void program();
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
program();
}
//===================//
// begin program //
//===================//
const int MX = 850;
const int N = (1<<20);
int n,s;
char ogr[MX][MX];
char gr[MX][MX];
int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};
queue<ii> bq, hq;
bool inside(int x, int y) {return x>=0 && x<n && y>=0 && y<n;}
bool moveHoney() {
queue<ii> nq;
while(!hq.empty()) {
int x = hq.front().fi;
int y = hq.front().se; hq.pop();
if(gr[x][y] != 'M') continue;
RE(d,4) {
int nx = x+dx[d];
int ny = y+dy[d];
if(!inside(nx,ny)) continue;
if(gr[nx][ny] == 'D') return true;
if(gr[nx][ny] != 'G') continue;
gr[nx][ny] = 'M';
nq.push({nx,ny});
}
}
hq = nq;
return false;
}
void moveBees() {
queue<ii> nq;
while(!bq.empty()) {
int x = bq.front().fi;
int y = bq.front().se; bq.pop();
RE(d,4) {
int nx = x+dx[d];
int ny = y+dy[d];
if(!inside(nx,ny)) continue;
if(gr[nx][ny] != 'G' && gr[nx][ny] != 'M') continue;
gr[nx][ny] = 'H';
nq.push({nx,ny});
}
}
bq = nq;
}
bool possible(int x) {
RE(i,n) RE(j,n) gr[i][j] = ogr[i][j];
while(!bq.empty()) bq.pop();
while(!hq.empty()) hq.pop();
RE(i,n) RE(j,n) if(gr[i][j]=='H') bq.push({i,j});
RE(i,n) RE(j,n) if(gr[i][j]=='M') hq.push({i,j});
RE(_,x) moveBees();
while(!hq.empty()) {
RE(_,s) if(moveHoney()) return true;
moveBees();
}
return false;
}
void program() {
IN(n,s);
RE(i,n) RE(j,n) IN(ogr[i][j]);
if(!possible(0)) {
OUTL(-1);
return;
}
int lb=0, ub=n*n;
while(lb != ub) {
int mid=(lb+ub+1)/2;
if(possible(mid)) lb=mid;
else ub=mid-1;
}
OUTL(lb);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
420 ms |
2664 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
2 ms |
336 KB |
Output is correct |
13 |
Correct |
2 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
2 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
2 ms |
332 KB |
Output is correct |
24 |
Correct |
2 ms |
332 KB |
Output is correct |
25 |
Correct |
3 ms |
332 KB |
Output is correct |
26 |
Correct |
2 ms |
328 KB |
Output is correct |
27 |
Correct |
3 ms |
332 KB |
Output is correct |
28 |
Correct |
3 ms |
332 KB |
Output is correct |
29 |
Correct |
4 ms |
460 KB |
Output is correct |
30 |
Correct |
3 ms |
460 KB |
Output is correct |
31 |
Correct |
4 ms |
460 KB |
Output is correct |
32 |
Correct |
3 ms |
464 KB |
Output is correct |
33 |
Correct |
110 ms |
1012 KB |
Output is correct |
34 |
Correct |
70 ms |
1000 KB |
Output is correct |
35 |
Correct |
55 ms |
972 KB |
Output is correct |
36 |
Correct |
140 ms |
1140 KB |
Output is correct |
37 |
Correct |
100 ms |
1108 KB |
Output is correct |
38 |
Correct |
74 ms |
1100 KB |
Output is correct |
39 |
Correct |
186 ms |
1268 KB |
Output is correct |
40 |
Correct |
129 ms |
1228 KB |
Output is correct |
41 |
Correct |
94 ms |
1220 KB |
Output is correct |
42 |
Correct |
233 ms |
1404 KB |
Output is correct |
43 |
Correct |
154 ms |
1400 KB |
Output is correct |
44 |
Correct |
116 ms |
1420 KB |
Output is correct |
45 |
Correct |
300 ms |
1484 KB |
Output is correct |
46 |
Correct |
200 ms |
1484 KB |
Output is correct |
47 |
Correct |
142 ms |
1496 KB |
Output is correct |
48 |
Correct |
333 ms |
1668 KB |
Output is correct |
49 |
Correct |
226 ms |
1696 KB |
Output is correct |
50 |
Correct |
168 ms |
1612 KB |
Output is correct |
51 |
Correct |
414 ms |
1860 KB |
Output is correct |
52 |
Correct |
272 ms |
1740 KB |
Output is correct |
53 |
Correct |
201 ms |
1740 KB |
Output is correct |
54 |
Correct |
482 ms |
1988 KB |
Output is correct |
55 |
Correct |
322 ms |
1988 KB |
Output is correct |
56 |
Correct |
234 ms |
1984 KB |
Output is correct |
57 |
Correct |
559 ms |
2240 KB |
Output is correct |
58 |
Correct |
371 ms |
2244 KB |
Output is correct |
59 |
Correct |
278 ms |
2124 KB |
Output is correct |
60 |
Correct |
624 ms |
2272 KB |
Output is correct |
61 |
Correct |
436 ms |
2272 KB |
Output is correct |
62 |
Correct |
324 ms |
2252 KB |
Output is correct |
63 |
Correct |
340 ms |
2276 KB |
Output is correct |
64 |
Correct |
369 ms |
2376 KB |
Output is correct |
65 |
Correct |
339 ms |
2272 KB |
Output is correct |
66 |
Correct |
364 ms |
2284 KB |
Output is correct |
67 |
Correct |
24 ms |
2252 KB |
Output is correct |
68 |
Correct |
356 ms |
2252 KB |
Output is correct |
69 |
Correct |
314 ms |
2372 KB |
Output is correct |
70 |
Correct |
369 ms |
2252 KB |
Output is correct |
71 |
Correct |
363 ms |
2372 KB |
Output is correct |
72 |
Correct |
371 ms |
2252 KB |
Output is correct |
73 |
Correct |
365 ms |
2940 KB |
Output is correct |
74 |
Correct |
425 ms |
2836 KB |
Output is correct |
75 |
Correct |
456 ms |
2940 KB |
Output is correct |
76 |
Correct |
456 ms |
2828 KB |
Output is correct |
77 |
Correct |
457 ms |
3000 KB |
Output is correct |
78 |
Correct |
30 ms |
2768 KB |
Output is correct |
79 |
Correct |
427 ms |
2964 KB |
Output is correct |
80 |
Correct |
441 ms |
2764 KB |
Output is correct |
81 |
Correct |
432 ms |
2928 KB |
Output is correct |
82 |
Correct |
427 ms |
2716 KB |
Output is correct |
83 |
Correct |
446 ms |
2836 KB |
Output is correct |
84 |
Correct |
450 ms |
2732 KB |
Output is correct |
85 |
Correct |
453 ms |
2848 KB |
Output is correct |
86 |
Correct |
458 ms |
2816 KB |
Output is correct |
87 |
Correct |
430 ms |
2688 KB |
Output is correct |
88 |
Correct |
431 ms |
2716 KB |
Output is correct |
89 |
Correct |
434 ms |
2500 KB |
Output is correct |
90 |
Correct |
442 ms |
2584 KB |
Output is correct |
91 |
Correct |
427 ms |
2596 KB |
Output is correct |
92 |
Correct |
428 ms |
2632 KB |
Output is correct |