#include <stdio.h>
#include <queue>
using namespace std;
const int N=805;
char Base[N][N];
int Move[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
queue<int> q;
int dist[N][N];
void BFS(int s)
{
while(q.size())
{
int x=q.front()/N;
int y=q.front()%N;
q.pop();
for(int i=0;i<4;i++)
{
int nx=x+Move[i][0];
int ny=y+Move[i][1];
if(Base[nx][ny]=='G' && dist[nx][ny]==-1)
{
dist[nx][ny]=dist[x][y]+s;
q.push(nx*N+ny);
}
}
}
}
int tmp[N][N];
bool Check(int n, int s, int sx, int sy, int fx, int fy, int mid)
{
int i,j;
for(i=1;i<=n;i++) for(j=1;j<=n;j++) tmp[i][j]=-1;
if(dist[sx][sy]<=mid*s) return false;
tmp[sx][sy]=mid*s;
q.push(sx*N+sy);
while(q.size())
{
int x=q.front()/N;
int y=q.front()%N;
q.pop();
for(i=0;i<4;i++)
{
int nx=x+Move[i][0];
int ny=y+Move[i][1];
if((Base[nx][ny]=='G' && tmp[nx][ny]==-1 && dist[nx][ny]>tmp[x][y]+1) || (Base[nx][ny]=='D' && tmp[nx][ny]==-1))
{
tmp[nx][ny]=tmp[x][y]+1;
q.push(nx*N+ny);
}
}
}
return tmp[fx][fy]!=-1;
}
int main()
{
int n,s,i,j,sx,sy,fx,fy;
scanf("%i %i",&n,&s);
for(i=1;i<=n;i++) for(j=1;j<=n;j++) dist[i][j]=-1;
for(i=1;i<=n;i++)
{
scanf("%s",Base[i]+1);
for(j=1;j<=n;j++)
{
if(Base[i][j]=='M') sx=i,sy=j,Base[i][j]='G';
if(Base[i][j]=='D') fx=i,fy=j;
if(Base[i][j]=='H') q.push(i*N+j),dist[i][j]=0;
}
}
BFS(s);
int top=n*n,bot=0,mid,ans=-1;
while(top>=bot)
{
mid=top+bot>>1;
if(Check(n,s,sx,sy,fx,fy,mid)) ans=mid,bot=mid+1;
else top=mid-1;
}
printf("%i\n",ans);
return 0;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:73:10: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
mid=top+bot>>1;
~~~^~~~
mecho.cpp:57:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i %i",&n,&s);
~~~~~^~~~~~~~~~~~~~~
mecho.cpp:61:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s",Base[i]+1);
~~~~~^~~~~~~~~~~~~~~~
mecho.cpp:74:11: warning: 'fy' may be used uninitialized in this function [-Wmaybe-uninitialized]
if(Check(n,s,sx,sy,fx,fy,mid)) ans=mid,bot=mid+1;
~~~~~^~~~~~~~~~~~~~~~~~~~~
mecho.cpp:74:11: warning: 'fx' may be used uninitialized in this function [-Wmaybe-uninitialized]
mecho.cpp:74:11: warning: 'sy' may be used uninitialized in this function [-Wmaybe-uninitialized]
mecho.cpp:74:11: warning: 'sx' may be used uninitialized in this function [-Wmaybe-uninitialized]
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
80 ms |
6748 KB |
Output is correct |
8 |
Correct |
3 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
512 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
2 ms |
512 KB |
Output is correct |
12 |
Correct |
2 ms |
768 KB |
Output is correct |
13 |
Correct |
3 ms |
768 KB |
Output is correct |
14 |
Correct |
3 ms |
768 KB |
Output is correct |
15 |
Correct |
2 ms |
512 KB |
Output is correct |
16 |
Correct |
2 ms |
512 KB |
Output is correct |
17 |
Correct |
2 ms |
512 KB |
Output is correct |
18 |
Correct |
2 ms |
512 KB |
Output is correct |
19 |
Correct |
2 ms |
512 KB |
Output is correct |
20 |
Correct |
2 ms |
512 KB |
Output is correct |
21 |
Correct |
2 ms |
588 KB |
Output is correct |
22 |
Correct |
2 ms |
640 KB |
Output is correct |
23 |
Correct |
2 ms |
768 KB |
Output is correct |
24 |
Correct |
2 ms |
640 KB |
Output is correct |
25 |
Correct |
2 ms |
768 KB |
Output is correct |
26 |
Correct |
3 ms |
768 KB |
Output is correct |
27 |
Correct |
2 ms |
768 KB |
Output is correct |
28 |
Correct |
2 ms |
768 KB |
Output is correct |
29 |
Correct |
3 ms |
896 KB |
Output is correct |
30 |
Correct |
2 ms |
768 KB |
Output is correct |
31 |
Correct |
2 ms |
896 KB |
Output is correct |
32 |
Correct |
2 ms |
896 KB |
Output is correct |
33 |
Correct |
6 ms |
2944 KB |
Output is correct |
34 |
Correct |
7 ms |
2944 KB |
Output is correct |
35 |
Correct |
20 ms |
2944 KB |
Output is correct |
36 |
Correct |
8 ms |
3328 KB |
Output is correct |
37 |
Correct |
8 ms |
3328 KB |
Output is correct |
38 |
Correct |
23 ms |
3328 KB |
Output is correct |
39 |
Correct |
9 ms |
3712 KB |
Output is correct |
40 |
Correct |
10 ms |
3712 KB |
Output is correct |
41 |
Correct |
36 ms |
3712 KB |
Output is correct |
42 |
Correct |
12 ms |
4096 KB |
Output is correct |
43 |
Correct |
12 ms |
4096 KB |
Output is correct |
44 |
Correct |
38 ms |
4096 KB |
Output is correct |
45 |
Correct |
12 ms |
4480 KB |
Output is correct |
46 |
Correct |
13 ms |
4480 KB |
Output is correct |
47 |
Correct |
54 ms |
4600 KB |
Output is correct |
48 |
Correct |
14 ms |
4992 KB |
Output is correct |
49 |
Correct |
14 ms |
4992 KB |
Output is correct |
50 |
Correct |
63 ms |
5112 KB |
Output is correct |
51 |
Correct |
15 ms |
5376 KB |
Output is correct |
52 |
Correct |
17 ms |
5376 KB |
Output is correct |
53 |
Correct |
85 ms |
5412 KB |
Output is correct |
54 |
Correct |
16 ms |
5760 KB |
Output is correct |
55 |
Correct |
20 ms |
5760 KB |
Output is correct |
56 |
Correct |
94 ms |
5880 KB |
Output is correct |
57 |
Correct |
19 ms |
6264 KB |
Output is correct |
58 |
Correct |
24 ms |
6272 KB |
Output is correct |
59 |
Correct |
126 ms |
6144 KB |
Output is correct |
60 |
Correct |
24 ms |
6656 KB |
Output is correct |
61 |
Correct |
26 ms |
6656 KB |
Output is correct |
62 |
Correct |
135 ms |
6776 KB |
Output is correct |
63 |
Correct |
119 ms |
6676 KB |
Output is correct |
64 |
Correct |
205 ms |
6720 KB |
Output is correct |
65 |
Correct |
152 ms |
6656 KB |
Output is correct |
66 |
Correct |
124 ms |
6648 KB |
Output is correct |
67 |
Correct |
122 ms |
6636 KB |
Output is correct |
68 |
Correct |
51 ms |
6632 KB |
Output is correct |
69 |
Correct |
49 ms |
6648 KB |
Output is correct |
70 |
Correct |
41 ms |
6648 KB |
Output is correct |
71 |
Correct |
44 ms |
6604 KB |
Output is correct |
72 |
Correct |
32 ms |
6648 KB |
Output is correct |
73 |
Correct |
34 ms |
6776 KB |
Output is correct |
74 |
Correct |
55 ms |
6784 KB |
Output is correct |
75 |
Correct |
75 ms |
6776 KB |
Output is correct |
76 |
Correct |
60 ms |
6776 KB |
Output is correct |
77 |
Correct |
60 ms |
6776 KB |
Output is correct |
78 |
Correct |
74 ms |
6776 KB |
Output is correct |
79 |
Correct |
50 ms |
6904 KB |
Output is correct |
80 |
Correct |
58 ms |
6776 KB |
Output is correct |
81 |
Correct |
84 ms |
6776 KB |
Output is correct |
82 |
Correct |
60 ms |
6908 KB |
Output is correct |
83 |
Correct |
79 ms |
6776 KB |
Output is correct |
84 |
Correct |
67 ms |
6784 KB |
Output is correct |
85 |
Correct |
87 ms |
6776 KB |
Output is correct |
86 |
Correct |
68 ms |
6776 KB |
Output is correct |
87 |
Correct |
70 ms |
6776 KB |
Output is correct |
88 |
Correct |
70 ms |
6648 KB |
Output is correct |
89 |
Correct |
75 ms |
6784 KB |
Output is correct |
90 |
Correct |
83 ms |
6776 KB |
Output is correct |
91 |
Correct |
78 ms |
6776 KB |
Output is correct |
92 |
Correct |
84 ms |
6784 KB |
Output is correct |