#include <bits/stdc++.h>
using namespace std;
long long n,s,si,sj,ei,ej;
long long a[1000][1000];
long long b[1000][1000];
long long c[1000][1000];
long long x[4]={0,0,-1,1};
long long y[4]={-1,1,0,0};
bool check(long long t)
{
queue<pair<pair<long long,long long>,pair<long long,long long> > > h;
bool ch=false;
h.push({{0,t},{si,sj}});
c[si][sj]=t;
while(!h.empty())
{
long long t0=h.front().first.first;
long long t1=h.front().first.second;
long long t2=h.front().second.first;
long long t3=h.front().second.second;
h.pop();
if(t2==ei && t3==ej)
{
while(!h.empty())
{
h.pop();
}
return true;
}
for(int i=0;i<4;i++)
{
if(-1<a[t2+x[i]][t3+y[i]] && a[t2+x[i]][t3+y[i]]<=t1+(t0+1)/s)
{
continue;
}
if(c[t2+x[i]][t3+y[i]]>t1+(t0+1)/s)
{
c[t2+x[i]][t3+y[i]]=t1+(t0+1)/s;
h.push({{(t0+1)%s,t1+(t0+1)/s},{t2+x[i],t3+y[i]}});
}
}
}
return false;
}
int main(void)
{
queue<pair<long long,pair<long long,long long> > > h;
cin>>n>>s;
for(int i=1;i<=n;i++)
{
string tt;
cin>>tt;
for(int j=1;j<=n;j++)
{
if(tt[j-1]=='T')
{
a[i][j]=-1;
b[i][j]=-1;
}
else
{
a[i][j]=INT_MAX;
b[i][j]=INT_MAX;
}
if(tt[j-1]=='M')
{
si=i;
sj=j;
}
else if(tt[j-1]=='D')
{
a[i][j]=-1;
ei=i;
ej=j;
}
else if(tt[j-1]=='H')
{
h.push({0,{i,j}});
a[i][j]=0;
}
}
}
while(!h.empty())
{
long long t1=h.front().first;
long long t2=h.front().second.first;
long long t3=h.front().second.second;
h.pop();
for(int i=0;i<4;i++)
{
if(a[t2+x[i]][t3+y[i]]>t1+1)
{
a[t2+x[i]][t3+y[i]]=t1+1;
h.push({t1+1,{t2+x[i],t3+y[i]}});
}
}
}
// for(int i=0;i<=n;i++)
// {
// for(int j=0;j<=n;j++)
// {
// if(a[i][j]==INT_MAX)
// {
// cout<<"-1 ";
// continue;
// }
// cout<<a[i][j]<<" ";
// }
// cout<<"\n";
// }
long long l=1,r=1000000;
while(l<=r)
{
long long mid=(l+r)/2;
for(int i=0;i<=n+1;i++)
{
for(int j=0;j<=n+1;j++)
{
c[i][j]=b[i][j];
}
}
if(check(mid))
{
l=mid+1;
}
else
{
r=mid-1;
}
}
// for(int i=0;i<=n;i++)
// {
// for(int j=0;j<=n;j++)
// {
// if(c[i][j]==INT_MAX)
// {
// cout<<"-1 ";
// continue;
// }
// cout<<c[i][j]<<" ";
// }
// cout<<"\n";
// }
cout<<r;
return 0;
}
/*
7 3
TTTTTTT
TGGGGGT
TGGGGGT
MGGGGGD
TGGGGGT
TGGGGGT
THHHHHT
TTTTTTT
T55555T
T44444T
M33333D
T22222T
T11111T
THHHHHT
TTTTTTT
T55555T
T44444T
M111222
T22222T
T11111T
THHHHHT
*/
Compilation message
mecho.cpp: In function 'bool check(long long int)':
mecho.cpp:15:7: warning: unused variable 'ch' [-Wunused-variable]
15 | bool ch=false;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
1 ms |
4432 KB |
Output is correct |
4 |
Correct |
1 ms |
4432 KB |
Output is correct |
5 |
Correct |
1 ms |
4600 KB |
Output is correct |
6 |
Correct |
1 ms |
4432 KB |
Output is correct |
7 |
Correct |
61 ms |
24144 KB |
Output is correct |
8 |
Incorrect |
1 ms |
4432 KB |
Output isn't correct |
9 |
Correct |
1 ms |
4548 KB |
Output is correct |
10 |
Correct |
1 ms |
4432 KB |
Output is correct |
11 |
Correct |
1 ms |
4432 KB |
Output is correct |
12 |
Incorrect |
2 ms |
6480 KB |
Output isn't correct |
13 |
Incorrect |
2 ms |
6480 KB |
Output isn't correct |
14 |
Incorrect |
2 ms |
6480 KB |
Output isn't correct |
15 |
Correct |
2 ms |
4432 KB |
Output is correct |
16 |
Correct |
1 ms |
4432 KB |
Output is correct |
17 |
Correct |
1 ms |
6480 KB |
Output is correct |
18 |
Correct |
1 ms |
6480 KB |
Output is correct |
19 |
Correct |
1 ms |
6704 KB |
Output is correct |
20 |
Correct |
1 ms |
6480 KB |
Output is correct |
21 |
Correct |
1 ms |
6480 KB |
Output is correct |
22 |
Correct |
2 ms |
6480 KB |
Output is correct |
23 |
Correct |
2 ms |
6480 KB |
Output is correct |
24 |
Correct |
1 ms |
6480 KB |
Output is correct |
25 |
Correct |
1 ms |
6480 KB |
Output is correct |
26 |
Correct |
1 ms |
6480 KB |
Output is correct |
27 |
Correct |
1 ms |
6480 KB |
Output is correct |
28 |
Correct |
1 ms |
6480 KB |
Output is correct |
29 |
Correct |
2 ms |
6480 KB |
Output is correct |
30 |
Correct |
1 ms |
6480 KB |
Output is correct |
31 |
Correct |
2 ms |
8528 KB |
Output is correct |
32 |
Correct |
2 ms |
8528 KB |
Output is correct |
33 |
Correct |
7 ms |
14928 KB |
Output is correct |
34 |
Correct |
9 ms |
14928 KB |
Output is correct |
35 |
Correct |
17 ms |
14928 KB |
Output is correct |
36 |
Correct |
8 ms |
16976 KB |
Output is correct |
37 |
Correct |
9 ms |
16976 KB |
Output is correct |
38 |
Correct |
21 ms |
17204 KB |
Output is correct |
39 |
Correct |
9 ms |
17232 KB |
Output is correct |
40 |
Correct |
9 ms |
17244 KB |
Output is correct |
41 |
Correct |
27 ms |
17256 KB |
Output is correct |
42 |
Correct |
11 ms |
17232 KB |
Output is correct |
43 |
Correct |
12 ms |
17232 KB |
Output is correct |
44 |
Correct |
33 ms |
17232 KB |
Output is correct |
45 |
Correct |
12 ms |
19280 KB |
Output is correct |
46 |
Correct |
12 ms |
19280 KB |
Output is correct |
47 |
Correct |
41 ms |
19392 KB |
Output is correct |
48 |
Correct |
15 ms |
21328 KB |
Output is correct |
49 |
Correct |
15 ms |
21328 KB |
Output is correct |
50 |
Correct |
48 ms |
21316 KB |
Output is correct |
51 |
Correct |
16 ms |
23376 KB |
Output is correct |
52 |
Correct |
17 ms |
23376 KB |
Output is correct |
53 |
Correct |
57 ms |
23624 KB |
Output is correct |
54 |
Correct |
17 ms |
23632 KB |
Output is correct |
55 |
Correct |
18 ms |
23632 KB |
Output is correct |
56 |
Correct |
64 ms |
23632 KB |
Output is correct |
57 |
Correct |
22 ms |
23632 KB |
Output is correct |
58 |
Correct |
19 ms |
23556 KB |
Output is correct |
59 |
Correct |
70 ms |
23636 KB |
Output is correct |
60 |
Correct |
21 ms |
23632 KB |
Output is correct |
61 |
Correct |
20 ms |
23632 KB |
Output is correct |
62 |
Correct |
80 ms |
23804 KB |
Output is correct |
63 |
Correct |
67 ms |
23816 KB |
Output is correct |
64 |
Correct |
110 ms |
23632 KB |
Output is correct |
65 |
Correct |
114 ms |
23828 KB |
Output is correct |
66 |
Correct |
102 ms |
23880 KB |
Output is correct |
67 |
Incorrect |
82 ms |
23660 KB |
Output isn't correct |
68 |
Correct |
37 ms |
23888 KB |
Output is correct |
69 |
Correct |
36 ms |
23888 KB |
Output is correct |
70 |
Correct |
35 ms |
23912 KB |
Output is correct |
71 |
Correct |
29 ms |
23880 KB |
Output is correct |
72 |
Incorrect |
29 ms |
23888 KB |
Output isn't correct |
73 |
Incorrect |
33 ms |
24400 KB |
Output isn't correct |
74 |
Correct |
50 ms |
24656 KB |
Output is correct |
75 |
Correct |
50 ms |
24400 KB |
Output is correct |
76 |
Correct |
46 ms |
24412 KB |
Output is correct |
77 |
Correct |
48 ms |
24392 KB |
Output is correct |
78 |
Incorrect |
53 ms |
24392 KB |
Output isn't correct |
79 |
Correct |
42 ms |
24400 KB |
Output is correct |
80 |
Correct |
46 ms |
24400 KB |
Output is correct |
81 |
Correct |
51 ms |
24416 KB |
Output is correct |
82 |
Correct |
47 ms |
24400 KB |
Output is correct |
83 |
Correct |
55 ms |
24284 KB |
Output is correct |
84 |
Correct |
53 ms |
24412 KB |
Output is correct |
85 |
Correct |
55 ms |
24364 KB |
Output is correct |
86 |
Correct |
57 ms |
24400 KB |
Output is correct |
87 |
Correct |
54 ms |
24288 KB |
Output is correct |
88 |
Correct |
57 ms |
24160 KB |
Output is correct |
89 |
Correct |
68 ms |
24144 KB |
Output is correct |
90 |
Correct |
61 ms |
24116 KB |
Output is correct |
91 |
Correct |
62 ms |
24144 KB |
Output is correct |
92 |
Correct |
60 ms |
24120 KB |
Output is correct |