#include <bits/stdc++.h>
using namespace std;
#define cin(vec) for(auto& i : vec) cin >> i
#define cout(vec) for(auto& i : vec) cout << i << " "; cout << "\n";
#define fast ios::sync_with_stdio(0);cin.tie(0);
#define loop(i,a,b) for (int i = a; i < b; i++)
#define F first
#define S second
#define pb(n) push_back(n)
#define pf(n) push_front(n)
#define dci(d) fixed<<setprecision(d)
#define sp ' '
#define el '\n'
#define all(v) v.begin(),v.end()
// #define int long long
int dx[8]= {0,0,1,-1,-1,1,1,-1};
int dy[8]= {-1,1,0,0,-1,1,-1,1};
int const N=8e2+5,M=1e2+5,Mod=1e9;
char grid[N][N];
int block[N][N],n,s;
bool vis[N][N];
int valid(int i,int j){
return (i>=0&&j>=0&&i<n&&j<n);
}
struct cell{
int i;
int j;
int t;
};
queue<cell>q;
void bfs(){
while(q.size()){
int i=q.front().i;
int j=q.front().j;
int t=q.front().t;
q.pop();
if(block[i][j]<=t){
continue;
}
block[i][j]=t;
for(int k=0;k<4;k++){
int x=dx[k]+i;
int y=dy[k]+j;
if(valid(x,y)&&grid[x][y]=='G'&&!vis[x][y]){
q.push({x,y,t+1});
}
}
}
}
bool bfs2(int i,int j,int st){
while(q.size())q.pop();
q.push({i,j,0});
vis[i][j]=1;
int t;
while(q.size()){
i=q.front().i;
j=q.front().j;
t=q.front().t;
q.pop();
if(grid[i][j]=='D')return 1;
for(int k=0;k<4;k++){
int x=dx[k]+i;
int y=dy[k]+j;
if(valid(x,y)&&(grid[x][y]=='G'||grid[x][y]=='D')&&block[x][y]>st+(t+1)/s&&!vis[x][y]){
q.push({x,y,t+1});
vis[x][y]=1;
}
}
}
return 0;
}
void testcase(int h){
cin>>n>>s;
int xst,yst,xed,yed;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
block[i][j]=1e9;
}
}
for(int i=0;i<n;i++){
string s;cin>>s;
for(int j=0;j<n;j++){
grid[i][j]=s[j];
if(s[j]=='T')
block[i][j]=-1;
if(s[j]=='H')
q.push({i,j,0});
if(s[j]=='M')xst=i,yst=j;
if(s[j]=='D')xed=i,yed=j;
}
}
bfs();
// for(int i=0;i<n;i++){
// for(int j=0;j<n;j++){
// cout<<block[i][j]<<' ';
// }cout<<el;
// }
int l=0,r=n*n;
int mx=-1;
while(l<=r){
int m=(l+r)/2;
// cout<<l<<' '<<m<<' '<<r<<el;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
vis[i][j]=0;
int x=bfs2(xst,yst,m);
if(x){
mx=m;
l=m+1;
}
else{
r=m-1;
}
}
cout<<mx<<el;
}
int32_t main()
{
// fast
testcase(1);
// int tc;cin>>tc;for(int i=1;i<=tc;i++)testcase(i);
return 0;
}
Compilation message
mecho.cpp: In function 'void testcase(int)':
mecho.cpp:74:15: warning: variable 'xed' set but not used [-Wunused-but-set-variable]
74 | int xst,yst,xed,yed;
| ^~~
mecho.cpp:74:19: warning: variable 'yed' set but not used [-Wunused-but-set-variable]
74 | int xst,yst,xed,yed;
| ^~~
mecho.cpp:106:15: warning: 'yst' may be used uninitialized in this function [-Wmaybe-uninitialized]
106 | int x=bfs2(xst,yst,m);
| ~~~~^~~~~~~~~~~
mecho.cpp:106:15: warning: 'xst' may be used uninitialized in this function [-Wmaybe-uninitialized]
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
110 ms |
4732 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
0 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
672 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
7 ms |
1876 KB |
Output is correct |
34 |
Correct |
7 ms |
1876 KB |
Output is correct |
35 |
Correct |
22 ms |
1980 KB |
Output is correct |
36 |
Correct |
7 ms |
2132 KB |
Output is correct |
37 |
Correct |
7 ms |
2132 KB |
Output is correct |
38 |
Correct |
30 ms |
2132 KB |
Output is correct |
39 |
Correct |
8 ms |
2388 KB |
Output is correct |
40 |
Correct |
9 ms |
2324 KB |
Output is correct |
41 |
Correct |
40 ms |
2364 KB |
Output is correct |
42 |
Correct |
11 ms |
2644 KB |
Output is correct |
43 |
Correct |
10 ms |
2644 KB |
Output is correct |
44 |
Correct |
48 ms |
2644 KB |
Output is correct |
45 |
Correct |
13 ms |
2900 KB |
Output is correct |
46 |
Correct |
16 ms |
2864 KB |
Output is correct |
47 |
Correct |
60 ms |
3020 KB |
Output is correct |
48 |
Correct |
16 ms |
3068 KB |
Output is correct |
49 |
Correct |
15 ms |
3156 KB |
Output is correct |
50 |
Correct |
99 ms |
3172 KB |
Output is correct |
51 |
Correct |
17 ms |
3360 KB |
Output is correct |
52 |
Correct |
18 ms |
3252 KB |
Output is correct |
53 |
Correct |
83 ms |
3404 KB |
Output is correct |
54 |
Correct |
20 ms |
3592 KB |
Output is correct |
55 |
Correct |
20 ms |
3540 KB |
Output is correct |
56 |
Correct |
98 ms |
3600 KB |
Output is correct |
57 |
Correct |
24 ms |
3772 KB |
Output is correct |
58 |
Correct |
30 ms |
3760 KB |
Output is correct |
59 |
Correct |
154 ms |
3796 KB |
Output is correct |
60 |
Correct |
26 ms |
4048 KB |
Output is correct |
61 |
Correct |
24 ms |
4052 KB |
Output is correct |
62 |
Correct |
127 ms |
4000 KB |
Output is correct |
63 |
Correct |
116 ms |
4080 KB |
Output is correct |
64 |
Correct |
214 ms |
3976 KB |
Output is correct |
65 |
Correct |
149 ms |
3980 KB |
Output is correct |
66 |
Correct |
125 ms |
4080 KB |
Output is correct |
67 |
Correct |
128 ms |
4092 KB |
Output is correct |
68 |
Correct |
67 ms |
4104 KB |
Output is correct |
69 |
Correct |
61 ms |
4164 KB |
Output is correct |
70 |
Correct |
48 ms |
4068 KB |
Output is correct |
71 |
Correct |
53 ms |
4172 KB |
Output is correct |
72 |
Incorrect |
49 ms |
4104 KB |
Output isn't correct |
73 |
Incorrect |
105 ms |
5712 KB |
Output isn't correct |
74 |
Correct |
90 ms |
5604 KB |
Output is correct |
75 |
Correct |
98 ms |
5828 KB |
Output is correct |
76 |
Correct |
88 ms |
5636 KB |
Output is correct |
77 |
Correct |
91 ms |
5664 KB |
Output is correct |
78 |
Correct |
108 ms |
5436 KB |
Output is correct |
79 |
Correct |
73 ms |
5564 KB |
Output is correct |
80 |
Correct |
92 ms |
5548 KB |
Output is correct |
81 |
Correct |
107 ms |
5452 KB |
Output is correct |
82 |
Correct |
88 ms |
5452 KB |
Output is correct |
83 |
Correct |
107 ms |
5180 KB |
Output is correct |
84 |
Correct |
106 ms |
5224 KB |
Output is correct |
85 |
Correct |
108 ms |
5296 KB |
Output is correct |
86 |
Correct |
99 ms |
5184 KB |
Output is correct |
87 |
Correct |
103 ms |
5272 KB |
Output is correct |
88 |
Correct |
117 ms |
4868 KB |
Output is correct |
89 |
Correct |
123 ms |
4908 KB |
Output is correct |
90 |
Correct |
120 ms |
4916 KB |
Output is correct |
91 |
Correct |
104 ms |
4840 KB |
Output is correct |
92 |
Correct |
111 ms |
4928 KB |
Output is correct |