#include <bits/stdc++.h>
using namespace std;
const long long MAX=810;
long long n,s;
char mat[MAX][MAX];
long long si,sj,ei,ej;
long long di[4]= {0,0,-1,1};
long long dj[4]= {1,-1,0,0};
vector<pair<long long,long long> > hives;
long long distmecho[MAX][MAX];
long long occupied[MAX][MAX];
bool canspreadhive(long long i, long long j)
{
if(i<0 || i>=n || j<0 || j>=n || occupied[i][j]!=-1 || mat[i][j]=='T' || mat[i][j]=='D')return false;
return true;
}
void spread_hives()
{
memset(occupied,-1,sizeof(occupied));
queue<pair<long long,long long> > q;
for(auto x:hives)
{
q.push(x);
occupied[x.first][x.second]=0;
}
while(!q.empty())
{
auto curr=q.front();
q.pop();
for(long long k=0; k<4; k++)
{
long long newi=curr.first+di[k];
long long newj=curr.second+dj[k];
if(!canspreadhive(newi,newj))continue;
occupied[newi][newj]=occupied[curr.first][curr.second]+1;
q.push({newi,newj});
}
}
}
bool canmechostep(long long i, long long j)
{
if(i<0 || i>=n || j<0 || j>=n || distmecho[i][j]!=-1 || mat[i][j]=='H' || mat[i][j]=='T')return false;
return true;
}
bool can_stay(long long X)
{
memset(distmecho,-1,sizeof(distmecho));
distmecho[si][sj]=0;
queue<pair<long long,long long> > q;
q.push({si,sj});
if(occupied[si][sj]<=X)return false;
while(!q.empty())
{
auto curr=q.front();
q.pop();
for(long long k=0; k<4; k++)
{
long long newi=curr.first+di[k];
long long newj=curr.second+dj[k];
if(!canmechostep(newi,newj))continue;
long long newtime=distmecho[curr.first][curr.second]+1;
long long tmp=newtime;
long long use=tmp/s+X;
if(tmp%s)use++;
if(newi==ei && newj==ej)return true;
if(use<=occupied[newi][newj])
{
distmecho[newi][newj]=newtime;
q.push({newi,newj});
}
}
}
if(distmecho[ei][ej]!=-1)return true;
else return false;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n>>s;
for(long long i=0; i<n; i++)
{
for(long long j=0; j<n; j++)
{
cin>>mat[i][j];
if(mat[i][j]=='M')
{
si=i;
sj=j;
}
else if(mat[i][j]=='D')
{
ei=i;
ej=j;
}
else if(mat[i][j]=='H')
{
hives.push_back({i,j});
}
}
}
spread_hives();
long long B=0,E=1e6;
long long res=-1;
while(B<=E)
{
long long mid=(B+E)/2;
if(can_stay(mid))
{
res=mid;
B=mid+1;
}
else
{
E=mid-1;
}
}
cout<<res<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
10844 KB |
Output isn't correct |
2 |
Incorrect |
5 ms |
11000 KB |
Output isn't correct |
3 |
Incorrect |
4 ms |
10764 KB |
Output isn't correct |
4 |
Incorrect |
4 ms |
11256 KB |
Output isn't correct |
5 |
Correct |
4 ms |
10844 KB |
Output is correct |
6 |
Correct |
4 ms |
10844 KB |
Output is correct |
7 |
Correct |
64 ms |
11404 KB |
Output is correct |
8 |
Incorrect |
4 ms |
10844 KB |
Output isn't correct |
9 |
Correct |
4 ms |
10844 KB |
Output is correct |
10 |
Correct |
4 ms |
11000 KB |
Output is correct |
11 |
Correct |
4 ms |
10844 KB |
Output is correct |
12 |
Correct |
4 ms |
10844 KB |
Output is correct |
13 |
Correct |
5 ms |
10844 KB |
Output is correct |
14 |
Correct |
4 ms |
10844 KB |
Output is correct |
15 |
Incorrect |
3 ms |
10844 KB |
Output isn't correct |
16 |
Correct |
5 ms |
10844 KB |
Output is correct |
17 |
Incorrect |
4 ms |
10844 KB |
Output isn't correct |
18 |
Correct |
4 ms |
10844 KB |
Output is correct |
19 |
Incorrect |
4 ms |
10844 KB |
Output isn't correct |
20 |
Correct |
3 ms |
10844 KB |
Output is correct |
21 |
Incorrect |
3 ms |
10844 KB |
Output isn't correct |
22 |
Correct |
4 ms |
10844 KB |
Output is correct |
23 |
Incorrect |
4 ms |
10844 KB |
Output isn't correct |
24 |
Correct |
3 ms |
10896 KB |
Output is correct |
25 |
Incorrect |
4 ms |
10892 KB |
Output isn't correct |
26 |
Correct |
3 ms |
10844 KB |
Output is correct |
27 |
Incorrect |
3 ms |
10844 KB |
Output isn't correct |
28 |
Correct |
3 ms |
10896 KB |
Output is correct |
29 |
Incorrect |
5 ms |
10892 KB |
Output isn't correct |
30 |
Correct |
4 ms |
10844 KB |
Output is correct |
31 |
Incorrect |
4 ms |
10844 KB |
Output isn't correct |
32 |
Correct |
5 ms |
10844 KB |
Output is correct |
33 |
Incorrect |
6 ms |
10844 KB |
Output isn't correct |
34 |
Correct |
6 ms |
10844 KB |
Output is correct |
35 |
Correct |
19 ms |
11016 KB |
Output is correct |
36 |
Incorrect |
7 ms |
11044 KB |
Output isn't correct |
37 |
Correct |
6 ms |
10964 KB |
Output is correct |
38 |
Correct |
26 ms |
11052 KB |
Output is correct |
39 |
Incorrect |
10 ms |
10844 KB |
Output isn't correct |
40 |
Correct |
9 ms |
10844 KB |
Output is correct |
41 |
Correct |
31 ms |
11100 KB |
Output is correct |
42 |
Incorrect |
9 ms |
10928 KB |
Output isn't correct |
43 |
Correct |
9 ms |
11124 KB |
Output is correct |
44 |
Correct |
36 ms |
11096 KB |
Output is correct |
45 |
Incorrect |
8 ms |
11164 KB |
Output isn't correct |
46 |
Correct |
9 ms |
10960 KB |
Output is correct |
47 |
Correct |
55 ms |
11156 KB |
Output is correct |
48 |
Incorrect |
9 ms |
11100 KB |
Output isn't correct |
49 |
Correct |
12 ms |
11164 KB |
Output is correct |
50 |
Correct |
48 ms |
11224 KB |
Output is correct |
51 |
Incorrect |
11 ms |
11096 KB |
Output isn't correct |
52 |
Correct |
10 ms |
11244 KB |
Output is correct |
53 |
Correct |
60 ms |
11244 KB |
Output is correct |
54 |
Incorrect |
14 ms |
11288 KB |
Output isn't correct |
55 |
Correct |
12 ms |
11096 KB |
Output is correct |
56 |
Correct |
91 ms |
11100 KB |
Output is correct |
57 |
Incorrect |
13 ms |
11100 KB |
Output isn't correct |
58 |
Correct |
13 ms |
11340 KB |
Output is correct |
59 |
Correct |
75 ms |
11352 KB |
Output is correct |
60 |
Incorrect |
17 ms |
11360 KB |
Output isn't correct |
61 |
Correct |
18 ms |
11380 KB |
Output is correct |
62 |
Correct |
126 ms |
11376 KB |
Output is correct |
63 |
Correct |
80 ms |
11364 KB |
Output is correct |
64 |
Correct |
125 ms |
11364 KB |
Output is correct |
65 |
Correct |
113 ms |
11356 KB |
Output is correct |
66 |
Incorrect |
106 ms |
11352 KB |
Output isn't correct |
67 |
Correct |
82 ms |
11352 KB |
Output is correct |
68 |
Correct |
42 ms |
11432 KB |
Output is correct |
69 |
Correct |
39 ms |
11436 KB |
Output is correct |
70 |
Correct |
44 ms |
11172 KB |
Output is correct |
71 |
Correct |
30 ms |
11168 KB |
Output is correct |
72 |
Correct |
27 ms |
11172 KB |
Output is correct |
73 |
Correct |
31 ms |
11752 KB |
Output is correct |
74 |
Correct |
51 ms |
11592 KB |
Output is correct |
75 |
Correct |
64 ms |
11704 KB |
Output is correct |
76 |
Correct |
55 ms |
11708 KB |
Output is correct |
77 |
Correct |
60 ms |
11708 KB |
Output is correct |
78 |
Correct |
58 ms |
11780 KB |
Output is correct |
79 |
Correct |
53 ms |
11888 KB |
Output is correct |
80 |
Correct |
48 ms |
11860 KB |
Output is correct |
81 |
Correct |
54 ms |
11640 KB |
Output is correct |
82 |
Correct |
49 ms |
11908 KB |
Output is correct |
83 |
Correct |
66 ms |
11764 KB |
Output is correct |
84 |
Correct |
64 ms |
11824 KB |
Output is correct |
85 |
Correct |
66 ms |
11556 KB |
Output is correct |
86 |
Correct |
62 ms |
11828 KB |
Output is correct |
87 |
Correct |
70 ms |
11820 KB |
Output is correct |
88 |
Correct |
62 ms |
11600 KB |
Output is correct |
89 |
Correct |
63 ms |
11612 KB |
Output is correct |
90 |
Correct |
96 ms |
11448 KB |
Output is correct |
91 |
Correct |
65 ms |
11448 KB |
Output is correct |
92 |
Correct |
60 ms |
11648 KB |
Output is correct |