#include <bits/stdc++.h>
using namespace std;
int n, s;
char ma[802][802];
int enj[802][802];
int pa[802][802];
int r, c;
int r2, c2;
int mov[4][2] = { {1, 0}, {0, 1}, {-1, 0}, {0, -1} };
bool isvalidBee(int x, int y)
{
if(!x || !y || x > n || y > n)
return false;
if(ma[x][y] == 'T' || ma[x][y] == 'D')
return false;
if(enj[x][y] != -1)
return false;
return true;
}
void BeeFS()
{
queue<pair<int, int> > q;
pair<int, int> act;
for(int i = 1; i <= n; ++i)
{
for(int j = 1; j <= n; ++j)
{
if(ma[i][j] == 'H')
{
q.push({i, j});
}
else
enj[i][j] = -1;
}
}
while(!q.empty())
{
act = q.front();
q.pop();
for(int i = 0; i < 4; ++i)
{
int nx = act.first + mov[i][0];
int ny = act.second + mov[i][1];
if(isvalidBee(nx, ny))
{
enj[nx][ny] = enj[act.first][act.second] + s;
q.push({nx, ny});
}
}
}
}
bool isvalidMecho(int x, int y, int time)
{
if(!x || !y || x > n || y > n)
return false;
if(ma[x][y] == 'T')
return false;
if(ma[x][y] == 'D')
return true;
if(pa[x][y] != -1)
return false;
if(time >= enj[x][y])
return false;
return true;
}
bool monotone(int m)
{
queue<pair<int, int> > q;
pair<int, int> act;
for( int i = 1; i <= n; i++ )
for( int j = 1; j <= n; j++ )
pa[i][j] = -1;
q.push({r, c});
pa[r][c] = m * s;
if(enj[r][c] <= pa[r][c]) return false;
while(!q.empty())
{
act = q.front();
q.pop();
int time = pa[act.first][act.second] + 1;
for(int i = 0; i < 4; ++i)
{
int nx = act.first + mov[i][0];
int ny = act.second + mov[i][1];
if(isvalidMecho(nx, ny, time))
{
q.push({nx, ny});
pa[nx][ny] = time;
}
}
}
return pa[r2][c2] != -1;
}
int Binary(int a, int b)
{
while(a != b)
{
int m = (a + b) / 2 + 1;
if(monotone(m))
{
a = m;
}
else
b = m - 1;
}
return a;
}
int main()
{
cin.tie(0) -> sync_with_stdio(0);
cin >> n >> s;
for(int i = 1; i <= n; ++i)
{
for(int j = 1; j <= n; ++j)
{
cin >> ma[i][j];
if(ma[i][j] == 'M')
{
r = i, c = j;
}
if(ma[i][j] == 'D')
{
r2 = i, c2 = j;
}
}
}
BeeFS();
if(!monotone(0))
{
cout << "-1\n"; return 0;
}
cout << Binary(0, 640000);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
74 ms |
6216 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
860 KB |
Output is correct |
13 |
Correct |
0 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
0 ms |
604 KB |
Output is correct |
16 |
Correct |
0 ms |
604 KB |
Output is correct |
17 |
Correct |
0 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
604 KB |
Output is correct |
19 |
Correct |
0 ms |
604 KB |
Output is correct |
20 |
Correct |
0 ms |
604 KB |
Output is correct |
21 |
Correct |
0 ms |
604 KB |
Output is correct |
22 |
Correct |
0 ms |
604 KB |
Output is correct |
23 |
Correct |
0 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
0 ms |
860 KB |
Output is correct |
26 |
Correct |
0 ms |
860 KB |
Output is correct |
27 |
Correct |
1 ms |
860 KB |
Output is correct |
28 |
Correct |
1 ms |
860 KB |
Output is correct |
29 |
Correct |
1 ms |
856 KB |
Output is correct |
30 |
Correct |
1 ms |
860 KB |
Output is correct |
31 |
Correct |
1 ms |
860 KB |
Output is correct |
32 |
Correct |
1 ms |
860 KB |
Output is correct |
33 |
Correct |
5 ms |
2908 KB |
Output is correct |
34 |
Correct |
5 ms |
2908 KB |
Output is correct |
35 |
Correct |
17 ms |
2836 KB |
Output is correct |
36 |
Correct |
6 ms |
3164 KB |
Output is correct |
37 |
Correct |
6 ms |
3164 KB |
Output is correct |
38 |
Correct |
22 ms |
3316 KB |
Output is correct |
39 |
Correct |
7 ms |
3420 KB |
Output is correct |
40 |
Correct |
8 ms |
3592 KB |
Output is correct |
41 |
Correct |
28 ms |
3420 KB |
Output is correct |
42 |
Correct |
8 ms |
3932 KB |
Output is correct |
43 |
Correct |
8 ms |
3932 KB |
Output is correct |
44 |
Correct |
34 ms |
4012 KB |
Output is correct |
45 |
Correct |
10 ms |
4188 KB |
Output is correct |
46 |
Correct |
11 ms |
4188 KB |
Output is correct |
47 |
Correct |
42 ms |
4188 KB |
Output is correct |
48 |
Correct |
11 ms |
4700 KB |
Output is correct |
49 |
Correct |
12 ms |
4628 KB |
Output is correct |
50 |
Correct |
49 ms |
4700 KB |
Output is correct |
51 |
Correct |
12 ms |
4956 KB |
Output is correct |
52 |
Correct |
14 ms |
4984 KB |
Output is correct |
53 |
Correct |
62 ms |
4956 KB |
Output is correct |
54 |
Correct |
14 ms |
5212 KB |
Output is correct |
55 |
Correct |
16 ms |
5424 KB |
Output is correct |
56 |
Correct |
69 ms |
5212 KB |
Output is correct |
57 |
Correct |
16 ms |
5720 KB |
Output is correct |
58 |
Correct |
19 ms |
5724 KB |
Output is correct |
59 |
Correct |
81 ms |
5724 KB |
Output is correct |
60 |
Correct |
20 ms |
5976 KB |
Output is correct |
61 |
Correct |
24 ms |
6116 KB |
Output is correct |
62 |
Correct |
90 ms |
5976 KB |
Output is correct |
63 |
Correct |
89 ms |
5980 KB |
Output is correct |
64 |
Correct |
143 ms |
6096 KB |
Output is correct |
65 |
Correct |
124 ms |
5980 KB |
Output is correct |
66 |
Correct |
103 ms |
5976 KB |
Output is correct |
67 |
Correct |
24 ms |
5980 KB |
Output is correct |
68 |
Correct |
36 ms |
5900 KB |
Output is correct |
69 |
Correct |
35 ms |
5972 KB |
Output is correct |
70 |
Correct |
28 ms |
5980 KB |
Output is correct |
71 |
Correct |
28 ms |
6156 KB |
Output is correct |
72 |
Correct |
28 ms |
5944 KB |
Output is correct |
73 |
Correct |
27 ms |
6192 KB |
Output is correct |
74 |
Correct |
46 ms |
6232 KB |
Output is correct |
75 |
Correct |
53 ms |
6232 KB |
Output is correct |
76 |
Correct |
51 ms |
6376 KB |
Output is correct |
77 |
Correct |
51 ms |
6192 KB |
Output is correct |
78 |
Correct |
27 ms |
6156 KB |
Output is correct |
79 |
Correct |
47 ms |
6164 KB |
Output is correct |
80 |
Correct |
51 ms |
6236 KB |
Output is correct |
81 |
Correct |
52 ms |
6236 KB |
Output is correct |
82 |
Correct |
50 ms |
6352 KB |
Output is correct |
83 |
Correct |
69 ms |
6312 KB |
Output is correct |
84 |
Correct |
60 ms |
6316 KB |
Output is correct |
85 |
Correct |
61 ms |
6224 KB |
Output is correct |
86 |
Correct |
57 ms |
6232 KB |
Output is correct |
87 |
Correct |
62 ms |
6308 KB |
Output is correct |
88 |
Correct |
65 ms |
6224 KB |
Output is correct |
89 |
Correct |
70 ms |
6232 KB |
Output is correct |
90 |
Correct |
64 ms |
6236 KB |
Output is correct |
91 |
Correct |
71 ms |
6236 KB |
Output is correct |
92 |
Correct |
62 ms |
6232 KB |
Output is correct |