#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*2;
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 |
10684 KB |
Output isn't correct |
2 |
Incorrect |
2 ms |
10844 KB |
Output isn't correct |
3 |
Incorrect |
2 ms |
10880 KB |
Output isn't correct |
4 |
Incorrect |
2 ms |
10688 KB |
Output isn't correct |
5 |
Correct |
2 ms |
10840 KB |
Output is correct |
6 |
Correct |
2 ms |
10844 KB |
Output is correct |
7 |
Correct |
72 ms |
11604 KB |
Output is correct |
8 |
Incorrect |
2 ms |
10844 KB |
Output isn't correct |
9 |
Correct |
2 ms |
10844 KB |
Output is correct |
10 |
Correct |
2 ms |
10688 KB |
Output is correct |
11 |
Correct |
2 ms |
10844 KB |
Output is correct |
12 |
Incorrect |
2 ms |
10844 KB |
Output isn't correct |
13 |
Incorrect |
2 ms |
10844 KB |
Output isn't correct |
14 |
Correct |
2 ms |
10844 KB |
Output is correct |
15 |
Incorrect |
2 ms |
10872 KB |
Output isn't correct |
16 |
Incorrect |
2 ms |
10844 KB |
Output isn't correct |
17 |
Incorrect |
2 ms |
10688 KB |
Output isn't correct |
18 |
Incorrect |
2 ms |
10840 KB |
Output isn't correct |
19 |
Incorrect |
2 ms |
10844 KB |
Output isn't correct |
20 |
Incorrect |
2 ms |
10844 KB |
Output isn't correct |
21 |
Incorrect |
2 ms |
10844 KB |
Output isn't correct |
22 |
Incorrect |
2 ms |
10844 KB |
Output isn't correct |
23 |
Incorrect |
2 ms |
10844 KB |
Output isn't correct |
24 |
Incorrect |
3 ms |
10844 KB |
Output isn't correct |
25 |
Incorrect |
2 ms |
10844 KB |
Output isn't correct |
26 |
Incorrect |
2 ms |
10844 KB |
Output isn't correct |
27 |
Incorrect |
2 ms |
10844 KB |
Output isn't correct |
28 |
Incorrect |
3 ms |
10844 KB |
Output isn't correct |
29 |
Incorrect |
3 ms |
10840 KB |
Output isn't correct |
30 |
Incorrect |
3 ms |
10844 KB |
Output isn't correct |
31 |
Incorrect |
3 ms |
10692 KB |
Output isn't correct |
32 |
Incorrect |
3 ms |
10844 KB |
Output isn't correct |
33 |
Incorrect |
12 ms |
10844 KB |
Output isn't correct |
34 |
Incorrect |
16 ms |
10840 KB |
Output isn't correct |
35 |
Correct |
18 ms |
10844 KB |
Output is correct |
36 |
Incorrect |
15 ms |
11136 KB |
Output isn't correct |
37 |
Incorrect |
20 ms |
11100 KB |
Output isn't correct |
38 |
Correct |
24 ms |
10960 KB |
Output is correct |
39 |
Incorrect |
19 ms |
11100 KB |
Output isn't correct |
40 |
Incorrect |
25 ms |
11100 KB |
Output isn't correct |
41 |
Correct |
31 ms |
11100 KB |
Output is correct |
42 |
Incorrect |
25 ms |
11356 KB |
Output isn't correct |
43 |
Incorrect |
31 ms |
11096 KB |
Output isn't correct |
44 |
Correct |
38 ms |
11356 KB |
Output is correct |
45 |
Incorrect |
29 ms |
11172 KB |
Output isn't correct |
46 |
Incorrect |
39 ms |
11096 KB |
Output isn't correct |
47 |
Correct |
44 ms |
11356 KB |
Output is correct |
48 |
Incorrect |
36 ms |
11344 KB |
Output isn't correct |
49 |
Incorrect |
48 ms |
11352 KB |
Output isn't correct |
50 |
Correct |
52 ms |
11352 KB |
Output is correct |
51 |
Incorrect |
39 ms |
11356 KB |
Output isn't correct |
52 |
Incorrect |
54 ms |
11460 KB |
Output isn't correct |
53 |
Correct |
62 ms |
11344 KB |
Output is correct |
54 |
Incorrect |
45 ms |
11352 KB |
Output isn't correct |
55 |
Incorrect |
61 ms |
11352 KB |
Output isn't correct |
56 |
Correct |
72 ms |
11352 KB |
Output is correct |
57 |
Incorrect |
56 ms |
11600 KB |
Output isn't correct |
58 |
Incorrect |
70 ms |
11648 KB |
Output isn't correct |
59 |
Correct |
82 ms |
11612 KB |
Output is correct |
60 |
Incorrect |
59 ms |
11604 KB |
Output isn't correct |
61 |
Incorrect |
99 ms |
11600 KB |
Output isn't correct |
62 |
Correct |
99 ms |
11612 KB |
Output is correct |
63 |
Correct |
91 ms |
11728 KB |
Output is correct |
64 |
Correct |
132 ms |
11712 KB |
Output is correct |
65 |
Correct |
130 ms |
11768 KB |
Output is correct |
66 |
Incorrect |
111 ms |
11716 KB |
Output isn't correct |
67 |
Correct |
96 ms |
11500 KB |
Output is correct |
68 |
Correct |
53 ms |
11584 KB |
Output is correct |
69 |
Correct |
49 ms |
11604 KB |
Output is correct |
70 |
Correct |
42 ms |
11612 KB |
Output is correct |
71 |
Correct |
42 ms |
11600 KB |
Output is correct |
72 |
Incorrect |
36 ms |
11600 KB |
Output isn't correct |
73 |
Incorrect |
51 ms |
12000 KB |
Output isn't correct |
74 |
Correct |
56 ms |
11996 KB |
Output is correct |
75 |
Correct |
56 ms |
12368 KB |
Output is correct |
76 |
Correct |
58 ms |
12120 KB |
Output is correct |
77 |
Correct |
67 ms |
12116 KB |
Output is correct |
78 |
Correct |
64 ms |
12044 KB |
Output is correct |
79 |
Correct |
56 ms |
12116 KB |
Output is correct |
80 |
Correct |
54 ms |
12008 KB |
Output is correct |
81 |
Correct |
63 ms |
12116 KB |
Output is correct |
82 |
Correct |
57 ms |
12040 KB |
Output is correct |
83 |
Correct |
69 ms |
11988 KB |
Output is correct |
84 |
Correct |
64 ms |
11860 KB |
Output is correct |
85 |
Correct |
80 ms |
12116 KB |
Output is correct |
86 |
Correct |
67 ms |
12112 KB |
Output is correct |
87 |
Correct |
67 ms |
12112 KB |
Output is correct |
88 |
Correct |
72 ms |
11860 KB |
Output is correct |
89 |
Correct |
71 ms |
11872 KB |
Output is correct |
90 |
Correct |
75 ms |
11864 KB |
Output is correct |
91 |
Correct |
71 ms |
11856 KB |
Output is correct |
92 |
Correct |
88 ms |
11860 KB |
Output is correct |