#include<iostream>
#include<algorithm>
#include<queue>
#define optimizar_io ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
int n, s;
int x, y, x2, y2;
char mapa[802][802];
int TB[802][802], TM[802][802];
int mov[4][2] = { {1, 0}, {0, 1}, {-1, 0}, {0, -1} };
bool valideBee( int r, int c ){
if( !r || !c || r > n || c > n )
return false;
if( TB[r][c] != -1 )
return false;
if( mapa[r][c] == 'T' || mapa[r][c] == 'D' )
return false;
return true;
}
void BeeFS(){
queue< pair< int, int > > Q;
pair< int, int > act;
int r, c;
for( int i = 1; i <= n; i++ )
for( int j = 1; j <= n; j++ ){
if( mapa[i][j] == 'H' )
Q.push( make_pair( i, j ) );
else
TB[i][j] = -1;
}
while( !Q.empty() ){
act = Q.front();
Q.pop();
for( int i = 0; i < 4; i++ ){
r = act.first + mov[i][0]; c = act.second + mov[i][1];
if( valideBee( r, c ) ){
TB[r][c] = TB[act.first][act.second] + s;
Q.push( make_pair( r, c ) );
}
}
}
}
bool valideMecho( int r, int c, int t ){
if( !r || !c || r > n || c > n )
return false;
if( TM[r][c] != -1 )
return false;
if( mapa[r][c] == 'T' )
return false;
if( mapa[r][c] == 'D' )
return true;
if( t >= TB[r][c] )
return false;
return true;
}
bool possible( int wait ){
queue< pair< int, int > > Q;
pair< int, int > act;
int r, c, t;
for( int i = 1; i <= n; i++ )
for( int j = 1; j <= n; j++ )
TM[i][j] = -1;
TM[x][y] = wait * s;
if( TB[x][y] <= TM[x][y] )
return false;
Q.push( make_pair( x, y ) );
while( !Q.empty() ){
act = Q.front();
Q.pop();
t = TM[act.first][act.second] + 1;
for( int i = 0; i < 4; i++ ){
r = act.first + mov[i][0]; c = act.second + mov[i][1];
if( valideMecho( r, c, t ) ){
TM[r][c] = t;
Q.push( make_pair( r, c ) );
}
}
}
return TM[x2][y2] != -1;
}
int main(){
optimizar_io
cin >> n >> s;
for( int i = 1; i <= n; i++ )
for( int j = 1; j <= n; j++ ){
cin >> mapa[i][j];
if( mapa[i][j] == 'M' ){
x = i; y = j;
}
if( mapa[i][j] == 'D' ){
x2 = i; y2 = j;
}
}
BeeFS();
/*for( int i = 1; i <= n; i++ ){
for( int j = 1; j <= n; j++ )
cout << TB[i][j] << " ";
cout << endl;
}
cout << endl;*/
if( !possible( 0 ) ){
cout << "-1\n";
return 0;
}
int b = 0, e = 640000, m;
while( b != e ){
m = (b + e) / 2 + 1;
if( possible( m ) )
b = m;
else
e = m - 1;
}
cout << b << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2512 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2516 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
67 ms |
6840 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
4564 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
4696 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
0 ms |
600 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
1 ms |
2652 KB |
Output is correct |
24 |
Correct |
1 ms |
2652 KB |
Output is correct |
25 |
Correct |
1 ms |
4700 KB |
Output is correct |
26 |
Correct |
1 ms |
4800 KB |
Output is correct |
27 |
Correct |
1 ms |
4700 KB |
Output is correct |
28 |
Correct |
1 ms |
4700 KB |
Output is correct |
29 |
Correct |
1 ms |
4700 KB |
Output is correct |
30 |
Correct |
1 ms |
4700 KB |
Output is correct |
31 |
Correct |
1 ms |
4696 KB |
Output is correct |
32 |
Correct |
1 ms |
4696 KB |
Output is correct |
33 |
Correct |
4 ms |
4952 KB |
Output is correct |
34 |
Correct |
7 ms |
4972 KB |
Output is correct |
35 |
Correct |
17 ms |
5152 KB |
Output is correct |
36 |
Correct |
6 ms |
5208 KB |
Output is correct |
37 |
Correct |
7 ms |
5212 KB |
Output is correct |
38 |
Correct |
21 ms |
4260 KB |
Output is correct |
39 |
Correct |
6 ms |
4444 KB |
Output is correct |
40 |
Correct |
8 ms |
3676 KB |
Output is correct |
41 |
Correct |
26 ms |
5308 KB |
Output is correct |
42 |
Correct |
7 ms |
5212 KB |
Output is correct |
43 |
Correct |
9 ms |
5212 KB |
Output is correct |
44 |
Correct |
33 ms |
5212 KB |
Output is correct |
45 |
Correct |
10 ms |
4956 KB |
Output is correct |
46 |
Correct |
12 ms |
4956 KB |
Output is correct |
47 |
Correct |
40 ms |
5468 KB |
Output is correct |
48 |
Correct |
11 ms |
5472 KB |
Output is correct |
49 |
Correct |
11 ms |
5464 KB |
Output is correct |
50 |
Correct |
48 ms |
5468 KB |
Output is correct |
51 |
Correct |
12 ms |
5920 KB |
Output is correct |
52 |
Correct |
14 ms |
5720 KB |
Output is correct |
53 |
Correct |
62 ms |
5760 KB |
Output is correct |
54 |
Correct |
14 ms |
5980 KB |
Output is correct |
55 |
Correct |
15 ms |
5980 KB |
Output is correct |
56 |
Correct |
68 ms |
6044 KB |
Output is correct |
57 |
Correct |
17 ms |
6236 KB |
Output is correct |
58 |
Correct |
20 ms |
6236 KB |
Output is correct |
59 |
Correct |
78 ms |
6492 KB |
Output is correct |
60 |
Correct |
19 ms |
6748 KB |
Output is correct |
61 |
Correct |
23 ms |
6488 KB |
Output is correct |
62 |
Correct |
93 ms |
6748 KB |
Output is correct |
63 |
Correct |
87 ms |
6484 KB |
Output is correct |
64 |
Correct |
129 ms |
6740 KB |
Output is correct |
65 |
Correct |
133 ms |
6484 KB |
Output is correct |
66 |
Correct |
96 ms |
6748 KB |
Output is correct |
67 |
Correct |
27 ms |
6484 KB |
Output is correct |
68 |
Correct |
45 ms |
6596 KB |
Output is correct |
69 |
Correct |
40 ms |
6744 KB |
Output is correct |
70 |
Correct |
30 ms |
6736 KB |
Output is correct |
71 |
Correct |
31 ms |
6736 KB |
Output is correct |
72 |
Correct |
27 ms |
6740 KB |
Output is correct |
73 |
Correct |
28 ms |
7004 KB |
Output is correct |
74 |
Correct |
57 ms |
6852 KB |
Output is correct |
75 |
Correct |
54 ms |
7004 KB |
Output is correct |
76 |
Correct |
46 ms |
6992 KB |
Output is correct |
77 |
Correct |
51 ms |
6804 KB |
Output is correct |
78 |
Correct |
24 ms |
7004 KB |
Output is correct |
79 |
Correct |
47 ms |
6748 KB |
Output is correct |
80 |
Correct |
49 ms |
6820 KB |
Output is correct |
81 |
Correct |
51 ms |
7004 KB |
Output is correct |
82 |
Correct |
46 ms |
7004 KB |
Output is correct |
83 |
Correct |
70 ms |
6748 KB |
Output is correct |
84 |
Correct |
58 ms |
6944 KB |
Output is correct |
85 |
Correct |
57 ms |
6744 KB |
Output is correct |
86 |
Correct |
64 ms |
6940 KB |
Output is correct |
87 |
Correct |
56 ms |
6788 KB |
Output is correct |
88 |
Correct |
65 ms |
6736 KB |
Output is correct |
89 |
Correct |
69 ms |
6892 KB |
Output is correct |
90 |
Correct |
61 ms |
6744 KB |
Output is correct |
91 |
Correct |
69 ms |
6892 KB |
Output is correct |
92 |
Correct |
64 ms |
6884 KB |
Output is correct |