#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});
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(use<=occupied[newi][newj] || (newi==ei && newj==ej))
{
distmecho[newi][newj]=newtime;
q.push({newi,newj});
}
}
}
if(distmecho[ei][ej]!=-1)return true;
else return false;
}
int main()
{
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 |
3 ms |
10840 KB |
Output isn't correct |
2 |
Incorrect |
3 ms |
10840 KB |
Output isn't correct |
3 |
Incorrect |
5 ms |
10844 KB |
Output isn't correct |
4 |
Incorrect |
3 ms |
10872 KB |
Output isn't correct |
5 |
Correct |
3 ms |
10772 KB |
Output is correct |
6 |
Correct |
3 ms |
10844 KB |
Output is correct |
7 |
Correct |
77 ms |
11552 KB |
Output is correct |
8 |
Incorrect |
3 ms |
10840 KB |
Output isn't correct |
9 |
Correct |
3 ms |
10844 KB |
Output is correct |
10 |
Correct |
3 ms |
10872 KB |
Output is correct |
11 |
Correct |
3 ms |
10844 KB |
Output is correct |
12 |
Incorrect |
3 ms |
10844 KB |
Output isn't correct |
13 |
Incorrect |
4 ms |
10840 KB |
Output isn't correct |
14 |
Correct |
3 ms |
10840 KB |
Output is correct |
15 |
Incorrect |
4 ms |
10844 KB |
Output isn't correct |
16 |
Correct |
4 ms |
10844 KB |
Output is correct |
17 |
Incorrect |
3 ms |
10844 KB |
Output isn't correct |
18 |
Correct |
3 ms |
10876 KB |
Output is correct |
19 |
Incorrect |
3 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 |
3 ms |
10844 KB |
Output is correct |
23 |
Incorrect |
3 ms |
10844 KB |
Output isn't correct |
24 |
Correct |
3 ms |
10844 KB |
Output is correct |
25 |
Incorrect |
4 ms |
10844 KB |
Output isn't correct |
26 |
Correct |
3 ms |
10844 KB |
Output is correct |
27 |
Incorrect |
4 ms |
10844 KB |
Output isn't correct |
28 |
Correct |
3 ms |
10844 KB |
Output is correct |
29 |
Incorrect |
3 ms |
10652 KB |
Output isn't correct |
30 |
Correct |
4 ms |
10844 KB |
Output is correct |
31 |
Incorrect |
3 ms |
10844 KB |
Output isn't correct |
32 |
Correct |
3 ms |
10844 KB |
Output is correct |
33 |
Incorrect |
8 ms |
10844 KB |
Output isn't correct |
34 |
Correct |
9 ms |
10952 KB |
Output is correct |
35 |
Correct |
20 ms |
10988 KB |
Output is correct |
36 |
Incorrect |
9 ms |
10844 KB |
Output isn't correct |
37 |
Correct |
9 ms |
10844 KB |
Output is correct |
38 |
Correct |
26 ms |
11028 KB |
Output is correct |
39 |
Incorrect |
11 ms |
10844 KB |
Output isn't correct |
40 |
Correct |
12 ms |
10844 KB |
Output is correct |
41 |
Correct |
31 ms |
10844 KB |
Output is correct |
42 |
Incorrect |
13 ms |
11096 KB |
Output isn't correct |
43 |
Correct |
14 ms |
11100 KB |
Output is correct |
44 |
Correct |
40 ms |
11116 KB |
Output is correct |
45 |
Incorrect |
18 ms |
11100 KB |
Output isn't correct |
46 |
Correct |
18 ms |
11040 KB |
Output is correct |
47 |
Correct |
49 ms |
11096 KB |
Output is correct |
48 |
Incorrect |
17 ms |
11100 KB |
Output isn't correct |
49 |
Correct |
17 ms |
11100 KB |
Output is correct |
50 |
Correct |
56 ms |
10988 KB |
Output is correct |
51 |
Incorrect |
20 ms |
11088 KB |
Output isn't correct |
52 |
Correct |
21 ms |
10956 KB |
Output is correct |
53 |
Correct |
81 ms |
11100 KB |
Output is correct |
54 |
Incorrect |
23 ms |
11100 KB |
Output isn't correct |
55 |
Correct |
24 ms |
11196 KB |
Output is correct |
56 |
Correct |
82 ms |
11100 KB |
Output is correct |
57 |
Incorrect |
23 ms |
11088 KB |
Output isn't correct |
58 |
Correct |
30 ms |
11092 KB |
Output is correct |
59 |
Correct |
100 ms |
11092 KB |
Output is correct |
60 |
Incorrect |
26 ms |
11092 KB |
Output isn't correct |
61 |
Correct |
27 ms |
11340 KB |
Output is correct |
62 |
Correct |
97 ms |
11344 KB |
Output is correct |
63 |
Correct |
92 ms |
11596 KB |
Output is correct |
64 |
Correct |
135 ms |
11096 KB |
Output is correct |
65 |
Correct |
126 ms |
11344 KB |
Output is correct |
66 |
Incorrect |
116 ms |
11332 KB |
Output isn't correct |
67 |
Correct |
98 ms |
11092 KB |
Output is correct |
68 |
Correct |
49 ms |
11344 KB |
Output is correct |
69 |
Correct |
51 ms |
11344 KB |
Output is correct |
70 |
Correct |
48 ms |
11344 KB |
Output is correct |
71 |
Correct |
41 ms |
11356 KB |
Output is correct |
72 |
Incorrect |
38 ms |
11356 KB |
Output isn't correct |
73 |
Incorrect |
42 ms |
11748 KB |
Output isn't correct |
74 |
Correct |
56 ms |
11704 KB |
Output is correct |
75 |
Correct |
62 ms |
11856 KB |
Output is correct |
76 |
Correct |
56 ms |
11860 KB |
Output is correct |
77 |
Correct |
59 ms |
11860 KB |
Output is correct |
78 |
Correct |
64 ms |
11856 KB |
Output is correct |
79 |
Correct |
55 ms |
11860 KB |
Output is correct |
80 |
Correct |
61 ms |
11624 KB |
Output is correct |
81 |
Correct |
61 ms |
11856 KB |
Output is correct |
82 |
Correct |
51 ms |
11856 KB |
Output is correct |
83 |
Correct |
90 ms |
11544 KB |
Output is correct |
84 |
Correct |
70 ms |
11748 KB |
Output is correct |
85 |
Correct |
62 ms |
11600 KB |
Output is correct |
86 |
Correct |
67 ms |
11608 KB |
Output is correct |
87 |
Correct |
68 ms |
11768 KB |
Output is correct |
88 |
Correct |
68 ms |
11604 KB |
Output is correct |
89 |
Correct |
75 ms |
11604 KB |
Output is correct |
90 |
Correct |
70 ms |
11604 KB |
Output is correct |
91 |
Correct |
70 ms |
11604 KB |
Output is correct |
92 |
Correct |
69 ms |
11604 KB |
Output is correct |