#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=n*n;
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;
}
Compilation message
mecho.cpp: In function 'bool can_stay(long long int)':
mecho.cpp:65:54: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
65 | if(use<=occupied[newi][newj] || newi==ei && newj==ej)
| ~~~~~~~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
10840 KB |
Output isn't correct |
2 |
Incorrect |
2 ms |
10844 KB |
Output isn't correct |
3 |
Incorrect |
2 ms |
10872 KB |
Output isn't correct |
4 |
Incorrect |
2 ms |
10844 KB |
Output isn't correct |
5 |
Correct |
2 ms |
10844 KB |
Output is correct |
6 |
Correct |
2 ms |
10628 KB |
Output is correct |
7 |
Correct |
70 ms |
11356 KB |
Output is correct |
8 |
Incorrect |
2 ms |
10840 KB |
Output isn't correct |
9 |
Correct |
2 ms |
10840 KB |
Output is correct |
10 |
Correct |
2 ms |
10840 KB |
Output is correct |
11 |
Correct |
2 ms |
10844 KB |
Output is correct |
12 |
Incorrect |
3 ms |
10840 KB |
Output isn't correct |
13 |
Incorrect |
4 ms |
10844 KB |
Output isn't correct |
14 |
Correct |
3 ms |
10892 KB |
Output is correct |
15 |
Incorrect |
2 ms |
10844 KB |
Output isn't correct |
16 |
Correct |
2 ms |
10844 KB |
Output is correct |
17 |
Incorrect |
2 ms |
10844 KB |
Output isn't correct |
18 |
Correct |
3 ms |
10844 KB |
Output is correct |
19 |
Incorrect |
2 ms |
10844 KB |
Output isn't correct |
20 |
Correct |
2 ms |
10844 KB |
Output is correct |
21 |
Incorrect |
2 ms |
10844 KB |
Output isn't correct |
22 |
Correct |
2 ms |
10844 KB |
Output is correct |
23 |
Incorrect |
3 ms |
10844 KB |
Output isn't correct |
24 |
Correct |
2 ms |
10844 KB |
Output is correct |
25 |
Incorrect |
3 ms |
10868 KB |
Output isn't correct |
26 |
Correct |
2 ms |
10876 KB |
Output is correct |
27 |
Incorrect |
3 ms |
10840 KB |
Output isn't correct |
28 |
Correct |
3 ms |
10840 KB |
Output is correct |
29 |
Incorrect |
3 ms |
10840 KB |
Output isn't correct |
30 |
Correct |
3 ms |
10844 KB |
Output is correct |
31 |
Incorrect |
3 ms |
10844 KB |
Output isn't correct |
32 |
Correct |
3 ms |
10840 KB |
Output is correct |
33 |
Incorrect |
11 ms |
10844 KB |
Output isn't correct |
34 |
Correct |
8 ms |
10844 KB |
Output is correct |
35 |
Correct |
20 ms |
10844 KB |
Output is correct |
36 |
Incorrect |
10 ms |
10844 KB |
Output isn't correct |
37 |
Correct |
10 ms |
10844 KB |
Output is correct |
38 |
Correct |
28 ms |
10844 KB |
Output is correct |
39 |
Incorrect |
14 ms |
10844 KB |
Output isn't correct |
40 |
Correct |
16 ms |
10844 KB |
Output is correct |
41 |
Correct |
35 ms |
10844 KB |
Output is correct |
42 |
Incorrect |
14 ms |
11104 KB |
Output isn't correct |
43 |
Correct |
15 ms |
11096 KB |
Output is correct |
44 |
Correct |
41 ms |
11100 KB |
Output is correct |
45 |
Incorrect |
15 ms |
11100 KB |
Output isn't correct |
46 |
Correct |
18 ms |
11100 KB |
Output is correct |
47 |
Correct |
44 ms |
11096 KB |
Output is correct |
48 |
Incorrect |
17 ms |
11096 KB |
Output isn't correct |
49 |
Correct |
18 ms |
11072 KB |
Output is correct |
50 |
Correct |
53 ms |
11100 KB |
Output is correct |
51 |
Incorrect |
20 ms |
11220 KB |
Output isn't correct |
52 |
Correct |
22 ms |
11100 KB |
Output is correct |
53 |
Correct |
66 ms |
11216 KB |
Output is correct |
54 |
Incorrect |
23 ms |
11096 KB |
Output isn't correct |
55 |
Correct |
25 ms |
11264 KB |
Output is correct |
56 |
Correct |
76 ms |
11256 KB |
Output is correct |
57 |
Incorrect |
27 ms |
11100 KB |
Output isn't correct |
58 |
Correct |
30 ms |
11100 KB |
Output is correct |
59 |
Correct |
90 ms |
11292 KB |
Output is correct |
60 |
Incorrect |
29 ms |
11100 KB |
Output isn't correct |
61 |
Correct |
30 ms |
11092 KB |
Output is correct |
62 |
Correct |
97 ms |
11088 KB |
Output is correct |
63 |
Correct |
89 ms |
11336 KB |
Output is correct |
64 |
Correct |
164 ms |
11336 KB |
Output is correct |
65 |
Correct |
122 ms |
11344 KB |
Output is correct |
66 |
Incorrect |
112 ms |
11344 KB |
Output isn't correct |
67 |
Correct |
97 ms |
11100 KB |
Output is correct |
68 |
Correct |
49 ms |
11344 KB |
Output is correct |
69 |
Correct |
48 ms |
11380 KB |
Output is correct |
70 |
Correct |
40 ms |
11344 KB |
Output is correct |
71 |
Correct |
45 ms |
11392 KB |
Output is correct |
72 |
Incorrect |
36 ms |
11348 KB |
Output isn't correct |
73 |
Incorrect |
43 ms |
11756 KB |
Output isn't correct |
74 |
Correct |
55 ms |
11748 KB |
Output is correct |
75 |
Correct |
64 ms |
11900 KB |
Output is correct |
76 |
Correct |
75 ms |
11860 KB |
Output is correct |
77 |
Correct |
56 ms |
11856 KB |
Output is correct |
78 |
Correct |
65 ms |
11868 KB |
Output is correct |
79 |
Correct |
50 ms |
11864 KB |
Output is correct |
80 |
Correct |
56 ms |
11660 KB |
Output is correct |
81 |
Correct |
61 ms |
11860 KB |
Output is correct |
82 |
Correct |
56 ms |
11852 KB |
Output is correct |
83 |
Correct |
69 ms |
11548 KB |
Output is correct |
84 |
Correct |
87 ms |
11776 KB |
Output is correct |
85 |
Correct |
72 ms |
11608 KB |
Output is correct |
86 |
Correct |
65 ms |
11764 KB |
Output is correct |
87 |
Correct |
93 ms |
11588 KB |
Output is correct |
88 |
Correct |
102 ms |
11596 KB |
Output is correct |
89 |
Correct |
75 ms |
11600 KB |
Output is correct |
90 |
Correct |
74 ms |
11664 KB |
Output is correct |
91 |
Correct |
112 ms |
11604 KB |
Output is correct |
92 |
Correct |
73 ms |
11416 KB |
Output is correct |