#include <bits/stdc++.h>
using namespace std;
//
#define ll long long int
#define endl "\n";
const int maxn = 801;
// array<int,n>
// pq
int n,st;
pair<int,int>mv[4]={ {0,1},{0,-1} , {1,0}, {-1,0} };
char g[maxn][maxn];
char g1[maxn][maxn];
char g2[maxn][maxn];
bool val(int x , int y){
if(x<0 || x>=n || y<0 || y>=n || g[x][y]=='T' )return 0;
return 1;
}
int xc,yc;
int xcc;
int ycc;
int beetime[maxn][maxn];
void bbfs(){
int vis[n][n];
queue<pair<pair<int,int>,int>>q;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
beetime[i][j]=INT_MAX;
if(g1[i][j]=='H'){
q.push({{i,j},0});
}
vis[i][j]=0;
}
}
while(!q.empty()){
pair<int,int>cur;
cur=q.front().first;
int dt = q.front().second;
q.pop();
if(vis[cur.first][cur.second])continue;
vis[cur.first][cur.second]=1;
if(g1[cur.first][cur.second]=='D' || g1[cur.first][cur.second]=='T'){
continue;
}
beetime[cur.first][cur.second]=dt;
// cout<<cur.first<<" "<<cur.second<<endl;
for(auto ele : mv){
int xx = cur.first+ele.first;
int yy = cur.second+ele.second;
if(val(xx,yy)){
q.push({{xx,yy},dt+1});
}
}
}
}
// int mbfs(int x){
bool tme(int marc , int bee){
int ccur = marc/st;
if(ccur*st!=marc){
return (marc/st)-1<bee;
}
return marc/st <bee;
}
bool check(int x){
bbfs();
queue<pair<pair<int,int>,int>>q;
bool vis[n][n];
int dist[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
vis[i][j]=0;
dist[i][j]=INT_MAX;
}
}
q.push({{xc,yc},0});
if(beetime[xc][yc]<=x)q.pop();
while(!q.empty()){
pair<int,int>cur=q.front().first;
int dt = q.front().second;
dist[cur.first][cur.second]=dt;
q.pop();
if(vis[cur.first][cur.second])continue;
vis[cur.first][cur.second]=1;
for(auto ele :mv){
int xx = cur.first+ele.first;
int yy = cur.second+ele.second;
if(val(xx,yy) && !vis[xx][yy] && tme(dt+1 , beetime[xx][yy]-x) && g[xx][yy]!='H'){
q.push({{xx,yy} , dt+1});
}
}
}
// for(int i=0;i<n;i++){
// for(int j=0;j<n;j++){
// cout<<vis[i][j]<<" ";
// }
// cout<<endl;
// }
for(auto ele : mv){
int xx = xcc+ele.first;
int yy = ycc+ele.second;
if(val(xx,yy) && vis[xx][yy]==1 && tme(dist[xx][yy] ,beetime[xx][yy]-x)){
// cout<<xx<<" "<<yy<<endl;
return 1;
}
}
return 0;
}
void solu(){
cin>>n>>st;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>g[i][j];
g1[i][j]=g[i][j];
if(g[i][j]=='M'){
xc=i;
yc=j;
}
if(g[i][j]=='D'){
xcc = i;
ycc = j;
}
}
}
// cout<<endl;
int l=0;
int r = 1e6;
int ans=0;
while(l<r){
int mid = l + (r-l+1)/2;
// cout<<mid<<endl;
if(check(mid)){
ans=max(mid,ans);
l=mid;
}else{
r=mid-1;
}
}
// cout<<check(2)<<endl;
cout<<ans<<endl;
}
int main(){
// ios_base::sync_with_stdio(0);
// cin.tie(0);
// int cass;
// cin>>cass;
// for(int i=0;i<cass;i++)
solu();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Incorrect |
0 ms |
2396 KB |
Output isn't correct |
6 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
7 |
Incorrect |
614 ms |
8744 KB |
Output isn't correct |
8 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
9 |
Correct |
1 ms |
2408 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
12 |
Correct |
2 ms |
4700 KB |
Output is correct |
13 |
Correct |
2 ms |
2904 KB |
Output is correct |
14 |
Incorrect |
4 ms |
4700 KB |
Output isn't correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
23 |
Correct |
1 ms |
2652 KB |
Output is correct |
24 |
Incorrect |
1 ms |
2496 KB |
Output isn't correct |
25 |
Correct |
2 ms |
4952 KB |
Output is correct |
26 |
Incorrect |
2 ms |
4700 KB |
Output isn't correct |
27 |
Correct |
2 ms |
4700 KB |
Output is correct |
28 |
Incorrect |
2 ms |
4700 KB |
Output isn't correct |
29 |
Correct |
2 ms |
4696 KB |
Output is correct |
30 |
Incorrect |
2 ms |
4740 KB |
Output isn't correct |
31 |
Correct |
2 ms |
4700 KB |
Output is correct |
32 |
Incorrect |
2 ms |
4700 KB |
Output isn't correct |
33 |
Correct |
35 ms |
5212 KB |
Output is correct |
34 |
Incorrect |
34 ms |
5212 KB |
Output isn't correct |
35 |
Incorrect |
80 ms |
5212 KB |
Output isn't correct |
36 |
Correct |
45 ms |
5620 KB |
Output is correct |
37 |
Incorrect |
43 ms |
5496 KB |
Output isn't correct |
38 |
Incorrect |
111 ms |
5468 KB |
Output isn't correct |
39 |
Correct |
57 ms |
5464 KB |
Output is correct |
40 |
Incorrect |
55 ms |
5468 KB |
Output isn't correct |
41 |
Incorrect |
133 ms |
5720 KB |
Output isn't correct |
42 |
Correct |
75 ms |
5916 KB |
Output is correct |
43 |
Incorrect |
74 ms |
5912 KB |
Output isn't correct |
44 |
Incorrect |
168 ms |
5940 KB |
Output isn't correct |
45 |
Correct |
84 ms |
5976 KB |
Output is correct |
46 |
Incorrect |
82 ms |
6072 KB |
Output isn't correct |
47 |
Incorrect |
214 ms |
6200 KB |
Output isn't correct |
48 |
Correct |
99 ms |
6332 KB |
Output is correct |
49 |
Incorrect |
96 ms |
6232 KB |
Output isn't correct |
50 |
Incorrect |
245 ms |
6492 KB |
Output isn't correct |
51 |
Correct |
118 ms |
6748 KB |
Output is correct |
52 |
Incorrect |
114 ms |
6736 KB |
Output isn't correct |
53 |
Incorrect |
299 ms |
6748 KB |
Output isn't correct |
54 |
Correct |
138 ms |
6996 KB |
Output is correct |
55 |
Incorrect |
134 ms |
7132 KB |
Output isn't correct |
56 |
Incorrect |
336 ms |
7248 KB |
Output isn't correct |
57 |
Correct |
153 ms |
7512 KB |
Output is correct |
58 |
Incorrect |
148 ms |
7520 KB |
Output isn't correct |
59 |
Incorrect |
387 ms |
7764 KB |
Output isn't correct |
60 |
Correct |
175 ms |
7764 KB |
Output is correct |
61 |
Incorrect |
168 ms |
7936 KB |
Output isn't correct |
62 |
Incorrect |
449 ms |
7972 KB |
Output isn't correct |
63 |
Correct |
413 ms |
7764 KB |
Output is correct |
64 |
Correct |
487 ms |
8020 KB |
Output is correct |
65 |
Incorrect |
480 ms |
8024 KB |
Output isn't correct |
66 |
Correct |
465 ms |
7952 KB |
Output is correct |
67 |
Incorrect |
427 ms |
8016 KB |
Output isn't correct |
68 |
Incorrect |
431 ms |
8024 KB |
Output isn't correct |
69 |
Correct |
437 ms |
8144 KB |
Output is correct |
70 |
Incorrect |
439 ms |
8020 KB |
Output isn't correct |
71 |
Incorrect |
422 ms |
8016 KB |
Output isn't correct |
72 |
Correct |
448 ms |
8276 KB |
Output is correct |
73 |
Correct |
589 ms |
9556 KB |
Output is correct |
74 |
Incorrect |
641 ms |
9588 KB |
Output isn't correct |
75 |
Incorrect |
631 ms |
9856 KB |
Output isn't correct |
76 |
Incorrect |
645 ms |
9620 KB |
Output isn't correct |
77 |
Incorrect |
629 ms |
9568 KB |
Output isn't correct |
78 |
Incorrect |
584 ms |
9524 KB |
Output isn't correct |
79 |
Incorrect |
612 ms |
9652 KB |
Output isn't correct |
80 |
Incorrect |
601 ms |
9412 KB |
Output isn't correct |
81 |
Incorrect |
598 ms |
9412 KB |
Output isn't correct |
82 |
Incorrect |
575 ms |
9656 KB |
Output isn't correct |
83 |
Incorrect |
609 ms |
9384 KB |
Output isn't correct |
84 |
Incorrect |
628 ms |
9304 KB |
Output isn't correct |
85 |
Incorrect |
611 ms |
9176 KB |
Output isn't correct |
86 |
Incorrect |
621 ms |
9300 KB |
Output isn't correct |
87 |
Incorrect |
625 ms |
9456 KB |
Output isn't correct |
88 |
Incorrect |
623 ms |
9380 KB |
Output isn't correct |
89 |
Incorrect |
631 ms |
8788 KB |
Output isn't correct |
90 |
Incorrect |
611 ms |
8784 KB |
Output isn't correct |
91 |
Incorrect |
618 ms |
9020 KB |
Output isn't correct |
92 |
Incorrect |
616 ms |
9128 KB |
Output isn't correct |