#include <bits/stdc++.h>
using namespace std;
int dx[4]={0,1,-1,0};
int dy[4]={1,0,0,-1};
string fields[805];
int beetime[805][805];
int mechotime[805][805];
int n,s;
bool check(int x,int y)
{
return (x>=0&&x<n&&y>=0&&y<n&&(fields[x][y]=='G'||fields[x][y]=='M'));
}
bool is_acc(int x,int y)
{
return ((x/s)<y);
}
int main()
{
cin>>n>>s;
for(int i=0;i<n;i++)cin>>fields[i];
int mechoposx,mechoposy;
int mechohomex,mechohomey;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
beetime[i][j]=-1;
}
}
queue<pair<int,int>>q;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(fields[i][j]=='M'){
mechoposx=i;
mechoposy=j;
}
if(fields[i][j]=='D'){
mechohomex=i;
mechohomey=j;
}
if(fields[i][j]=='H'){
beetime[i][j]=0;
q.push({i,j});
}
}
}
while(q.size()){
int x=q.front().first;
int y=q.front().second;
q.pop();
for(int i=0;i<4;i++){
int nx=x+dx[i];
int ny=y+dy[i];
if(check(nx,ny)&&beetime[nx][ny]==-1){
beetime[nx][ny]=beetime[x][y]+1;
q.push({nx,ny});
}
}
}
int l=0,r=n*n;
int res=0;
while(l<=r){
int eating_time=(l+r)/2;
q.push({mechoposx,mechoposy});
if(beetime[mechoposx][mechoposy]<=eating_time)q.pop();
bool acc=false;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
mechotime[i][j]=-1;
}
}
mechotime[mechoposx][mechoposy]=0;
while(q.size()){
int x=q.front().first;
int y=q.front().second;
q.pop();
for(int i=0;i<4;i++)
{
int nx=x+dx[i];
int ny=y+dy[i];
if(check(nx,ny)&&mechotime[nx][ny]==-1&&is_acc(mechotime[x][y]+1,beetime[nx][ny]-eating_time)) {
mechotime[nx][ny]=mechotime[x][y]+1;
q.push({nx,ny});
}
}
}
for(int i=0;i<4;i++){
int nx=mechohomex+dx[i];
int ny=mechohomey+dy[i];
if(check(nx,ny)&&mechotime[nx][ny]!=-1){
acc=true;
break;
}
}
if(acc){
res=eating_time;
l=eating_time+1;
}
else r=eating_time-1;
}
cout<<res;
return 0;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:71:40: warning: 'mechoposy' may be used uninitialized in this function [-Wmaybe-uninitialized]
71 | mechotime[mechoposx][mechoposy]=0;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
mecho.cpp:71:40: warning: 'mechoposx' may be used uninitialized in this function [-Wmaybe-uninitialized]
mecho.cpp:88:17: warning: 'mechohomey' may be used uninitialized in this function [-Wmaybe-uninitialized]
88 | int ny=mechohomey+dy[i];
| ^~
mecho.cpp:87:17: warning: 'mechohomex' may be used uninitialized in this function [-Wmaybe-uninitialized]
87 | int nx=mechohomex+dx[i];
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
65 ms |
6264 KB |
Output is correct |
8 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
9 |
Correct |
1 ms |
2516 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Incorrect |
1 ms |
4748 KB |
Output isn't correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2392 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2652 KB |
Output is correct |
24 |
Correct |
1 ms |
2652 KB |
Output is correct |
25 |
Correct |
1 ms |
4700 KB |
Output is correct |
26 |
Correct |
1 ms |
4700 KB |
Output is correct |
27 |
Correct |
1 ms |
4700 KB |
Output is correct |
28 |
Correct |
1 ms |
4700 KB |
Output is correct |
29 |
Correct |
1 ms |
4700 KB |
Output is correct |
30 |
Correct |
1 ms |
4700 KB |
Output is correct |
31 |
Correct |
1 ms |
4700 KB |
Output is correct |
32 |
Correct |
1 ms |
4568 KB |
Output is correct |
33 |
Correct |
5 ms |
4956 KB |
Output is correct |
34 |
Correct |
5 ms |
4956 KB |
Output is correct |
35 |
Correct |
17 ms |
4992 KB |
Output is correct |
36 |
Correct |
6 ms |
4956 KB |
Output is correct |
37 |
Correct |
7 ms |
4956 KB |
Output is correct |
38 |
Correct |
22 ms |
5028 KB |
Output is correct |
39 |
Correct |
7 ms |
4956 KB |
Output is correct |
40 |
Correct |
8 ms |
5100 KB |
Output is correct |
41 |
Correct |
40 ms |
4956 KB |
Output is correct |
42 |
Correct |
9 ms |
5208 KB |
Output is correct |
43 |
Correct |
9 ms |
5212 KB |
Output is correct |
44 |
Correct |
38 ms |
5212 KB |
Output is correct |
45 |
Correct |
11 ms |
5468 KB |
Output is correct |
46 |
Correct |
11 ms |
5468 KB |
Output is correct |
47 |
Correct |
41 ms |
5476 KB |
Output is correct |
48 |
Correct |
17 ms |
5464 KB |
Output is correct |
49 |
Correct |
13 ms |
5556 KB |
Output is correct |
50 |
Correct |
52 ms |
5640 KB |
Output is correct |
51 |
Correct |
15 ms |
5724 KB |
Output is correct |
52 |
Correct |
16 ms |
5932 KB |
Output is correct |
53 |
Correct |
62 ms |
5736 KB |
Output is correct |
54 |
Correct |
17 ms |
5980 KB |
Output is correct |
55 |
Correct |
17 ms |
5980 KB |
Output is correct |
56 |
Correct |
74 ms |
5980 KB |
Output is correct |
57 |
Correct |
19 ms |
6204 KB |
Output is correct |
58 |
Correct |
21 ms |
6236 KB |
Output is correct |
59 |
Correct |
101 ms |
6420 KB |
Output is correct |
60 |
Correct |
21 ms |
6492 KB |
Output is correct |
61 |
Correct |
22 ms |
6492 KB |
Output is correct |
62 |
Correct |
94 ms |
6484 KB |
Output is correct |
63 |
Correct |
91 ms |
6664 KB |
Output is correct |
64 |
Correct |
168 ms |
6640 KB |
Output is correct |
65 |
Correct |
123 ms |
6696 KB |
Output is correct |
66 |
Correct |
102 ms |
6892 KB |
Output is correct |
67 |
Incorrect |
113 ms |
6480 KB |
Output isn't correct |
68 |
Correct |
50 ms |
6740 KB |
Output is correct |
69 |
Correct |
42 ms |
6480 KB |
Output is correct |
70 |
Correct |
33 ms |
6544 KB |
Output is correct |
71 |
Correct |
38 ms |
6612 KB |
Output is correct |
72 |
Correct |
28 ms |
6480 KB |
Output is correct |
73 |
Correct |
29 ms |
6796 KB |
Output is correct |
74 |
Correct |
49 ms |
6736 KB |
Output is correct |
75 |
Correct |
58 ms |
7004 KB |
Output is correct |
76 |
Correct |
61 ms |
6740 KB |
Output is correct |
77 |
Correct |
59 ms |
6784 KB |
Output is correct |
78 |
Incorrect |
71 ms |
6760 KB |
Output isn't correct |
79 |
Correct |
42 ms |
6748 KB |
Output is correct |
80 |
Correct |
53 ms |
6888 KB |
Output is correct |
81 |
Correct |
56 ms |
6892 KB |
Output is correct |
82 |
Correct |
51 ms |
6844 KB |
Output is correct |
83 |
Correct |
63 ms |
6736 KB |
Output is correct |
84 |
Correct |
55 ms |
6484 KB |
Output is correct |
85 |
Correct |
63 ms |
6736 KB |
Output is correct |
86 |
Correct |
60 ms |
6912 KB |
Output is correct |
87 |
Correct |
60 ms |
6736 KB |
Output is correct |
88 |
Correct |
64 ms |
6696 KB |
Output is correct |
89 |
Correct |
63 ms |
6816 KB |
Output is correct |
90 |
Correct |
66 ms |
6860 KB |
Output is correct |
91 |
Correct |
70 ms |
6840 KB |
Output is correct |
92 |
Correct |
69 ms |
6736 KB |
Output is correct |