#include <bits/stdc++.h> //Andrei Alexandru a.k.a Sho
#define ll long long
#define double long double
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#define aint(a) (a).begin(), (a).end()
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define pi pair
#define rc(s) return cout<<s,0
#define endl '\n'
#define mod 998244353
#define PI 3.14159265359
#define INF 1000000005
#define LINF 1000000000000000005ll
#define CODE_START ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
ll n,s,sx,sy,dbees[805][805],d[805][805],viz[805][805],fx,fy;
pair<ll,ll>direction[5];
char a[805][805];
vector<pair<ll,ll>>hive;
ll check(ll x,ll y){
if(x>=1&&y>=1&&x<=n&&y<=n){
return 1;
}else return 0;
}
ll verif(ll time){
for(ll i=1;i<=n;i++)
for(ll j=1;j<=n;j++)
{
d[i][j]=LINF;
viz[i][j]=0;
}
queue<pair<ll,ll>>q;
d[sx][sy]=0;
viz[sx][sy]=1;
q.push(mp(sx,sy));
if(dbees[sx][sy]<=time){
q.pop();
}
while(!q.empty()){
ll x,y;
x=q.front().f;
y=q.front().s;
//cout<<x<<' '<<y<<endl;
q.pop();
for(ll i=0;i<4;i++)
{
ll xx,yy;
xx=x+direction[i].f;
yy=y+direction[i].s;
if(a[xx][yy]=='T'||a[xx][yy]=='H'){
continue;
}
if(check(xx,yy)==0){
continue;
}
if(viz[xx][yy]==1){
continue;
}
if((d[x][y]+1)/s>=dbees[xx][yy]-time){
continue;
}
viz[xx][yy]=1;
d[xx][yy]=d[x][y]+1;
q.push(mp(xx,yy));
}
}
ll ans=0;
for(ll i=0;i<4;i++)
{
ll x=fx+direction[i].f,y=fy+direction[i].s;
if(check(x,y)==0){
continue;
}
if(viz[x][y]==0){
continue;
}
if((d[x][y]/s)<dbees[x][y]-time){
ans=1;
}
}
return ans;
}
int32_t main(){
//CODE_START;
//ifstream cin("input.in");
direction[0]=mp(-1,0);
direction[1]=mp(1,0);
direction[2]=mp(0,-1);
direction[3]=mp(0,1);
cin>>n>>s;
for(ll i=1;i<=n;i++)
{
for(ll j=1;j<=n;j++)
{
cin>>a[i][j];
if(a[i][j]=='M'){
sx=i;
sy=j;
}else if(a[i][j]=='H'){
hive.pb(mp(i,j));
}else if(a[i][j]=='D'){
fx=i;
fy=j;
}
}
}
for(ll i=1;i<=n;i++)
{
for(ll j=1;j<=n;j++)
{
dbees[i][j]=LINF;
}
}
queue<pair<ll,ll>>q;
for(auto it : hive){
q.push(it);
dbees[it.f][it.s]=0;
}
while(!q.empty()){
ll x,y;
x=q.front().f;
y=q.front().s;
q.pop();
for(ll i=0;i<4;i++)
{
ll xx,yy;
xx=x+direction[i].f;
yy=y+direction[i].s;
if(a[xx][yy]=='T'||a[xx][yy]=='D'||a[xx][yy]=='H'){
continue;
}
if(check(xx,yy)&&viz[xx][yy]==0){
viz[xx][yy]=1;
q.push(mp(xx,yy));
dbees[xx][yy]=dbees[x][y]+1;
}
}
}
ll l=0,r=n*n,ans=-1;
//cout<<"DA"<<endl;
while(l<=r){
ll mid=(l+r)/2;
// cout<<mid<<endl;
if(verif(mid)){
l=mid+1;
ans=mid;
}else {
r=mid-1;
}
}
cout<<ans<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
119 ms |
16164 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
0 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
1100 KB |
Output is correct |
13 |
Correct |
1 ms |
972 KB |
Output is correct |
14 |
Correct |
1 ms |
1100 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
0 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
588 KB |
Output is correct |
18 |
Correct |
0 ms |
588 KB |
Output is correct |
19 |
Correct |
1 ms |
588 KB |
Output is correct |
20 |
Correct |
1 ms |
716 KB |
Output is correct |
21 |
Correct |
1 ms |
844 KB |
Output is correct |
22 |
Correct |
1 ms |
844 KB |
Output is correct |
23 |
Correct |
1 ms |
972 KB |
Output is correct |
24 |
Correct |
1 ms |
972 KB |
Output is correct |
25 |
Correct |
1 ms |
1100 KB |
Output is correct |
26 |
Correct |
1 ms |
1100 KB |
Output is correct |
27 |
Correct |
1 ms |
1228 KB |
Output is correct |
28 |
Correct |
1 ms |
1228 KB |
Output is correct |
29 |
Correct |
1 ms |
1228 KB |
Output is correct |
30 |
Correct |
1 ms |
1248 KB |
Output is correct |
31 |
Correct |
1 ms |
1356 KB |
Output is correct |
32 |
Correct |
1 ms |
1356 KB |
Output is correct |
33 |
Correct |
14 ms |
7208 KB |
Output is correct |
34 |
Correct |
14 ms |
7212 KB |
Output is correct |
35 |
Correct |
24 ms |
7196 KB |
Output is correct |
36 |
Correct |
15 ms |
8120 KB |
Output is correct |
37 |
Correct |
15 ms |
8140 KB |
Output is correct |
38 |
Correct |
32 ms |
8140 KB |
Output is correct |
39 |
Correct |
19 ms |
9164 KB |
Output is correct |
40 |
Correct |
21 ms |
9164 KB |
Output is correct |
41 |
Correct |
43 ms |
9176 KB |
Output is correct |
42 |
Correct |
23 ms |
10060 KB |
Output is correct |
43 |
Correct |
23 ms |
10156 KB |
Output is correct |
44 |
Correct |
53 ms |
10048 KB |
Output is correct |
45 |
Correct |
30 ms |
11084 KB |
Output is correct |
46 |
Correct |
30 ms |
11108 KB |
Output is correct |
47 |
Correct |
62 ms |
11112 KB |
Output is correct |
48 |
Correct |
34 ms |
12048 KB |
Output is correct |
49 |
Correct |
36 ms |
12056 KB |
Output is correct |
50 |
Correct |
75 ms |
12108 KB |
Output is correct |
51 |
Correct |
41 ms |
13040 KB |
Output is correct |
52 |
Correct |
45 ms |
13084 KB |
Output is correct |
53 |
Correct |
108 ms |
13124 KB |
Output is correct |
54 |
Correct |
47 ms |
14068 KB |
Output is correct |
55 |
Correct |
51 ms |
14060 KB |
Output is correct |
56 |
Correct |
115 ms |
14016 KB |
Output is correct |
57 |
Correct |
55 ms |
15052 KB |
Output is correct |
58 |
Correct |
57 ms |
14952 KB |
Output is correct |
59 |
Correct |
135 ms |
15076 KB |
Output is correct |
60 |
Correct |
64 ms |
16036 KB |
Output is correct |
61 |
Correct |
67 ms |
15960 KB |
Output is correct |
62 |
Correct |
153 ms |
15992 KB |
Output is correct |
63 |
Correct |
137 ms |
15968 KB |
Output is correct |
64 |
Correct |
210 ms |
15968 KB |
Output is correct |
65 |
Correct |
172 ms |
16012 KB |
Output is correct |
66 |
Correct |
150 ms |
15944 KB |
Output is correct |
67 |
Correct |
149 ms |
15956 KB |
Output is correct |
68 |
Correct |
87 ms |
16068 KB |
Output is correct |
69 |
Correct |
88 ms |
16076 KB |
Output is correct |
70 |
Correct |
78 ms |
16056 KB |
Output is correct |
71 |
Correct |
96 ms |
16068 KB |
Output is correct |
72 |
Correct |
74 ms |
16052 KB |
Output is correct |
73 |
Correct |
76 ms |
16636 KB |
Output is correct |
74 |
Correct |
95 ms |
16580 KB |
Output is correct |
75 |
Correct |
108 ms |
16584 KB |
Output is correct |
76 |
Correct |
113 ms |
16592 KB |
Output is correct |
77 |
Correct |
102 ms |
16552 KB |
Output is correct |
78 |
Correct |
110 ms |
16580 KB |
Output is correct |
79 |
Correct |
88 ms |
16452 KB |
Output is correct |
80 |
Correct |
100 ms |
16496 KB |
Output is correct |
81 |
Correct |
104 ms |
16572 KB |
Output is correct |
82 |
Correct |
97 ms |
16700 KB |
Output is correct |
83 |
Correct |
115 ms |
16452 KB |
Output is correct |
84 |
Correct |
125 ms |
16416 KB |
Output is correct |
85 |
Correct |
113 ms |
16384 KB |
Output is correct |
86 |
Correct |
109 ms |
16368 KB |
Output is correct |
87 |
Correct |
115 ms |
16580 KB |
Output is correct |
88 |
Correct |
114 ms |
16324 KB |
Output is correct |
89 |
Correct |
113 ms |
16328 KB |
Output is correct |
90 |
Correct |
118 ms |
16352 KB |
Output is correct |
91 |
Correct |
115 ms |
16368 KB |
Output is correct |
92 |
Correct |
137 ms |
16516 KB |
Output is correct |