#include<bits/stdc++.h>
using namespace std;
#define X first
#define Y second
int n,m,stx,sty,edx,edy,bee[805][805],st,md,ed,d[805][805],bear[805][805];
int dx[]={1,-1,0,0},dy[]={0,0,1,-1},u,v,x,y;
char s[805][805];
queue<pair<int,int> > q;
void debug(int kk)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(bee[i][j]==1e9)
{
printf("-1 ");
continue;
}
printf("%d ",bee[i][j]);
}
printf("\n");
}
printf("\n");
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(bear[i][j]==1e9)
{
printf("-1 ");
continue;
}
printf("%d ",bear[i][j]/m+kk);
}
printf("\n");
}
printf("\n");
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%s",s[i]+1);
}
for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)bee[i][j]=1e9,bear[i][j]=1e9;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(s[i][j]=='M')
{
stx=i,sty=j;
}else if(s[i][j]=='D')
{
edx=i,edy=j;
}else if(s[i][j]=='H')
{
q.push({i,j});
bee[i][j]=0;
}
}
}
while(!q.empty())
{
u=q.front().X,v=q.front().Y;
q.pop();
for(int i=0;i<4;i++)
{
x=u+dx[i],y=v+dy[i];
if(x>=1&&x<=n&&y>=1&&y<=n)
{
if((s[x][y]=='G'||s[x][y]=='M')&&bee[x][y]==1e9)
{
bee[x][y]=bee[u][v]+1;
q.push({x,y});
}
}
}
}
/*
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(bee[i][j]==1e9)
{
printf("-1 ");
continue;
}
printf("%d ",bee[i][j]);
}
printf("\n");
}
printf("\n");
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(bear[i][j]==1e9)
{
printf("-1 ");
continue;
}
printf("%d ",bear[i][j]);
}
printf("\n");
}
printf("\n");
*/
st=0;
ed=1e9;
while(ed>=st)
{
md=(st+ed)/2;
//printf("%d %d %d\n",st,md,ed);
memset(d,-1,sizeof d);
for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)bear[i][j]=1e9;
d[stx][sty]=0;
bear[stx][sty]=0;
q.push({stx,sty});
while(!q.empty())
{
u=q.front().X,v=q.front().Y;
q.pop();
for(int i=0;i<4;i++)
{
x=u+dx[i],y=v+dy[i];
if(x>=1&&x<=n&&y>=1&&y<=n)
{
if((s[x][y]=='G'||s[x][y]=='M'||s[x][y]=='D')&&d[x][y]==-1)
{
if((bear[u][v]+1)/m+md<bee[x][y])
{
d[x][y]=0;
bear[x][y]=bear[u][v]+1;
q.push({x,y});
}
}
}
}
}
//debug(md);
if(d[edx][edy]==0&&md<bee[stx][sty])
{
st=md+1;
}else
{
ed=md-1;
}
}
//printf("%d %d %d\n",st,md,ed);
printf("%d\n",ed);
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:42:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | scanf("%d%d",&n,&m);
| ~~~~~^~~~~~~~~~~~~~
mecho.cpp:45:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
45 | scanf("%s",s[i]+1);
| ~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2772 KB |
Output is correct |
2 |
Correct |
3 ms |
2772 KB |
Output is correct |
3 |
Correct |
3 ms |
2772 KB |
Output is correct |
4 |
Correct |
3 ms |
2900 KB |
Output is correct |
5 |
Correct |
3 ms |
2900 KB |
Output is correct |
6 |
Correct |
3 ms |
2900 KB |
Output is correct |
7 |
Correct |
92 ms |
8612 KB |
Output is correct |
8 |
Correct |
3 ms |
2900 KB |
Output is correct |
9 |
Correct |
4 ms |
2900 KB |
Output is correct |
10 |
Correct |
5 ms |
2900 KB |
Output is correct |
11 |
Correct |
3 ms |
2900 KB |
Output is correct |
12 |
Correct |
3 ms |
3284 KB |
Output is correct |
13 |
Correct |
3 ms |
3156 KB |
Output is correct |
14 |
Correct |
4 ms |
3284 KB |
Output is correct |
15 |
Correct |
3 ms |
2900 KB |
Output is correct |
16 |
Correct |
3 ms |
2900 KB |
Output is correct |
17 |
Correct |
3 ms |
3028 KB |
Output is correct |
18 |
Correct |
5 ms |
3028 KB |
Output is correct |
19 |
Correct |
3 ms |
3028 KB |
Output is correct |
20 |
Correct |
3 ms |
3028 KB |
Output is correct |
21 |
Correct |
3 ms |
3028 KB |
Output is correct |
22 |
Correct |
3 ms |
3028 KB |
Output is correct |
23 |
Correct |
3 ms |
3212 KB |
Output is correct |
24 |
Correct |
3 ms |
3156 KB |
Output is correct |
25 |
Correct |
3 ms |
3284 KB |
Output is correct |
26 |
Correct |
4 ms |
3192 KB |
Output is correct |
27 |
Correct |
3 ms |
3284 KB |
Output is correct |
28 |
Correct |
3 ms |
3284 KB |
Output is correct |
29 |
Correct |
3 ms |
3284 KB |
Output is correct |
30 |
Correct |
4 ms |
3284 KB |
Output is correct |
31 |
Correct |
4 ms |
3284 KB |
Output is correct |
32 |
Correct |
4 ms |
3284 KB |
Output is correct |
33 |
Correct |
9 ms |
5336 KB |
Output is correct |
34 |
Correct |
9 ms |
5328 KB |
Output is correct |
35 |
Correct |
25 ms |
5228 KB |
Output is correct |
36 |
Correct |
10 ms |
5588 KB |
Output is correct |
37 |
Correct |
9 ms |
5696 KB |
Output is correct |
38 |
Correct |
35 ms |
5692 KB |
Output is correct |
39 |
Correct |
11 ms |
5972 KB |
Output is correct |
40 |
Correct |
11 ms |
5972 KB |
Output is correct |
41 |
Correct |
43 ms |
6024 KB |
Output is correct |
42 |
Correct |
12 ms |
6288 KB |
Output is correct |
43 |
Correct |
15 ms |
6396 KB |
Output is correct |
44 |
Correct |
46 ms |
6356 KB |
Output is correct |
45 |
Correct |
16 ms |
6740 KB |
Output is correct |
46 |
Correct |
13 ms |
6740 KB |
Output is correct |
47 |
Correct |
89 ms |
6720 KB |
Output is correct |
48 |
Correct |
18 ms |
7000 KB |
Output is correct |
49 |
Correct |
16 ms |
7000 KB |
Output is correct |
50 |
Correct |
72 ms |
7092 KB |
Output is correct |
51 |
Correct |
18 ms |
7380 KB |
Output is correct |
52 |
Correct |
21 ms |
7460 KB |
Output is correct |
53 |
Correct |
79 ms |
7360 KB |
Output is correct |
54 |
Correct |
22 ms |
7820 KB |
Output is correct |
55 |
Correct |
24 ms |
7760 KB |
Output is correct |
56 |
Correct |
133 ms |
7804 KB |
Output is correct |
57 |
Correct |
21 ms |
8148 KB |
Output is correct |
58 |
Correct |
20 ms |
8056 KB |
Output is correct |
59 |
Correct |
112 ms |
8152 KB |
Output is correct |
60 |
Correct |
24 ms |
8516 KB |
Output is correct |
61 |
Correct |
24 ms |
8516 KB |
Output is correct |
62 |
Correct |
132 ms |
8404 KB |
Output is correct |
63 |
Correct |
110 ms |
8500 KB |
Output is correct |
64 |
Correct |
172 ms |
8524 KB |
Output is correct |
65 |
Correct |
168 ms |
8508 KB |
Output is correct |
66 |
Correct |
140 ms |
8500 KB |
Output is correct |
67 |
Correct |
147 ms |
8504 KB |
Output is correct |
68 |
Correct |
51 ms |
8532 KB |
Output is correct |
69 |
Correct |
50 ms |
8528 KB |
Output is correct |
70 |
Correct |
38 ms |
8536 KB |
Output is correct |
71 |
Correct |
38 ms |
8436 KB |
Output is correct |
72 |
Correct |
35 ms |
8452 KB |
Output is correct |
73 |
Correct |
51 ms |
8780 KB |
Output is correct |
74 |
Correct |
74 ms |
8788 KB |
Output is correct |
75 |
Correct |
67 ms |
8780 KB |
Output is correct |
76 |
Correct |
62 ms |
8800 KB |
Output is correct |
77 |
Correct |
66 ms |
8776 KB |
Output is correct |
78 |
Correct |
70 ms |
8668 KB |
Output is correct |
79 |
Correct |
63 ms |
8656 KB |
Output is correct |
80 |
Correct |
74 ms |
8660 KB |
Output is correct |
81 |
Correct |
70 ms |
8648 KB |
Output is correct |
82 |
Correct |
59 ms |
8644 KB |
Output is correct |
83 |
Correct |
82 ms |
8780 KB |
Output is correct |
84 |
Correct |
84 ms |
8716 KB |
Output is correct |
85 |
Correct |
74 ms |
8716 KB |
Output is correct |
86 |
Correct |
90 ms |
8780 KB |
Output is correct |
87 |
Correct |
78 ms |
8644 KB |
Output is correct |
88 |
Correct |
81 ms |
8572 KB |
Output is correct |
89 |
Correct |
81 ms |
8676 KB |
Output is correct |
90 |
Correct |
83 ms |
8588 KB |
Output is correct |
91 |
Correct |
80 ms |
8660 KB |
Output is correct |
92 |
Correct |
86 ms |
8664 KB |
Output is correct |