#include <iostream>
using namespace std;
int n,s;
char map[1005][1005],tmap[1005][1005],tag[1005][1005];
int ex,ey,beetag[1005][1005],beartag[1005][1005],sx,sy,bee[1000005][3],bear[1000005][3];
int check(int step)
{
//cout<<step<<endl;
int beep=0,beepp=-1,bearp=0,bearpp=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
tag[i][j]=0;
beartag[i][j]=0;
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(map[i][j]=='H')
{
beepp++;
bee[beepp][0]=i;
bee[beepp][1]=j;
bee[beepp][2]=0;
}
if(map[i][j]=='M')
{
bear[0][0]=i;
bear[0][1]=j;
bear[0][2]=0;
tag[i][j]=1;
map[i][j]='G';
}
}
}
while(beep<=beepp)
{
if(bee[beep][2]==step)break;
int nowx=bee[beep][0],nowy=bee[beep][1];
for(int i=1;i<=4;i++)
{
int tmpx=nowx,tmpy=nowy;
if(i==1)tmpx++;
if(i==2)tmpy++;
if(i==3)tmpx--;
if(i==4)tmpy--;
if((map[tmpx][tmpy]=='G' || map[tmpx][tmpy]=='M') && tmpx>0 && tmpy<=n && tmpy>0 && tmpy<=n)
{
beepp++;
bee[beepp][0]=tmpx;
bee[beepp][1]=tmpy;
bee[beepp][2]=bee[beep][2]+1;
map[tmpx][tmpy]='H';
}
}
beep++;
}
for(int i=1;i<=n;i++)
{
//for(int j=1;j<=n;j++)cout<<map[i][j];
//cout<<endl;
}
int count=n*2,bearstep=0;
while(count--)
{
bearstep+=s;
while(bearp<=bearpp)
{
//cout<<bear[bearp][0]<<" "<<bear[bearp][1]<<" "<<bear[bearp][2]<<endl;
if(bear[bearp][2]==bearstep)break;
int nowx=bear[bearp][0],nowy=bear[bearp][1];
if(map[nowx][nowy]=='H')
{
bearp++;
continue;
}
for(int i=1;i<=4;i++)
{
int tmpx=nowx,tmpy=nowy;
if(i==1)tmpx++;
if(i==2)tmpy++;
if(i==3)tmpx--;
if(i==4)tmpy--;
if(map[tmpx][tmpy]=='D')return 1;
if(map[tmpx][tmpy]=='G' && tmpx>0 && tmpy<=n && tmpy>0 && tmpy<=n && tag[tmpx][tmpy]==0)
{
bearpp++;
bear[bearpp][0]=tmpx;
bear[bearpp][1]=tmpy;
bear[bearpp][2]=bear[bearp][2]+1;
tag[tmpx][tmpy]=1;
}
}
bearp++;
}
step++;
//cout<<step<<" "<<count<<endl;
while(beep<=beepp)
{
//cout<<bee[beep][0]<<" "<<bee[beep][1]<<" "<<bee[beep][2]<<endl;
if(bee[beep][2]==step)break;
int nowx=bee[beep][0],nowy=bee[beep][1];
for(int i=1;i<=4;i++)
{
int tmpx=nowx,tmpy=nowy;
if(i==1)tmpx++;
if(i==2)tmpy++;
if(i==3)tmpx--;
if(i==4)tmpy--;
if(map[tmpx][tmpy]=='G' && tmpx>0 && tmpy<=n && tmpy>0 && tmpy<=n)
{
beepp++;
bee[beepp][0]=tmpx;
bee[beepp][1]=tmpy;
bee[beepp][2]=bee[beep][2]+1;
map[tmpx][tmpy]='H';
}
}
beep++;
}
for(int i=1;i<=n;i++)
{
//for(int j=1;j<=n;j++)cout<<map[i][j];
//cout<<endl;
}
//cout<<endl;
}
return 0;
}
int main()
{
cin>>n>>s;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cin>>map[i][j];
tmap[i][j]=map[i][j];
}
}
int l=0,r=n*n,ans=-1;
while(l<=r)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
map[i][j]=tmap[i][j];
}
}
int mid=(l+r)/2;
if(check(mid)==1)
{
ans=mid;
l=mid+1;
}
else
{
r=mid-1;
}
}
cout<<ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
504 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
364 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
403 ms |
17308 KB |
Output is correct |
8 |
Correct |
2 ms |
508 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
504 KB |
Output is correct |
11 |
Correct |
2 ms |
504 KB |
Output is correct |
12 |
Correct |
3 ms |
760 KB |
Output is correct |
13 |
Correct |
3 ms |
760 KB |
Output is correct |
14 |
Correct |
4 ms |
888 KB |
Output is correct |
15 |
Correct |
3 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
504 KB |
Output is correct |
17 |
Correct |
2 ms |
504 KB |
Output is correct |
18 |
Correct |
2 ms |
504 KB |
Output is correct |
19 |
Correct |
2 ms |
504 KB |
Output is correct |
20 |
Correct |
3 ms |
504 KB |
Output is correct |
21 |
Correct |
3 ms |
760 KB |
Output is correct |
22 |
Correct |
3 ms |
632 KB |
Output is correct |
23 |
Correct |
4 ms |
760 KB |
Output is correct |
24 |
Correct |
3 ms |
764 KB |
Output is correct |
25 |
Correct |
3 ms |
760 KB |
Output is correct |
26 |
Correct |
3 ms |
760 KB |
Output is correct |
27 |
Correct |
3 ms |
888 KB |
Output is correct |
28 |
Correct |
3 ms |
888 KB |
Output is correct |
29 |
Correct |
4 ms |
888 KB |
Output is correct |
30 |
Correct |
4 ms |
888 KB |
Output is correct |
31 |
Correct |
4 ms |
888 KB |
Output is correct |
32 |
Correct |
4 ms |
888 KB |
Output is correct |
33 |
Correct |
39 ms |
3576 KB |
Output is correct |
34 |
Correct |
41 ms |
3612 KB |
Output is correct |
35 |
Correct |
64 ms |
5112 KB |
Output is correct |
36 |
Correct |
50 ms |
4216 KB |
Output is correct |
37 |
Correct |
56 ms |
4216 KB |
Output is correct |
38 |
Correct |
83 ms |
6008 KB |
Output is correct |
39 |
Correct |
64 ms |
4856 KB |
Output is correct |
40 |
Correct |
72 ms |
4856 KB |
Output is correct |
41 |
Correct |
103 ms |
7192 KB |
Output is correct |
42 |
Correct |
82 ms |
5368 KB |
Output is correct |
43 |
Correct |
80 ms |
5368 KB |
Output is correct |
44 |
Correct |
132 ms |
8312 KB |
Output is correct |
45 |
Correct |
110 ms |
6120 KB |
Output is correct |
46 |
Correct |
99 ms |
6136 KB |
Output is correct |
47 |
Correct |
157 ms |
9564 KB |
Output is correct |
48 |
Correct |
114 ms |
6776 KB |
Output is correct |
49 |
Correct |
121 ms |
6776 KB |
Output is correct |
50 |
Correct |
192 ms |
11000 KB |
Output is correct |
51 |
Correct |
140 ms |
7416 KB |
Output is correct |
52 |
Correct |
141 ms |
7416 KB |
Output is correct |
53 |
Correct |
224 ms |
12360 KB |
Output is correct |
54 |
Correct |
160 ms |
8184 KB |
Output is correct |
55 |
Correct |
177 ms |
8312 KB |
Output is correct |
56 |
Correct |
270 ms |
13860 KB |
Output is correct |
57 |
Correct |
182 ms |
8952 KB |
Output is correct |
58 |
Correct |
187 ms |
9012 KB |
Output is correct |
59 |
Correct |
334 ms |
15504 KB |
Output is correct |
60 |
Correct |
220 ms |
9720 KB |
Output is correct |
61 |
Correct |
211 ms |
9848 KB |
Output is correct |
62 |
Correct |
346 ms |
17144 KB |
Output is correct |
63 |
Incorrect |
387 ms |
14200 KB |
Output isn't correct |
64 |
Correct |
433 ms |
14816 KB |
Output is correct |
65 |
Correct |
419 ms |
14584 KB |
Output is correct |
66 |
Correct |
409 ms |
13688 KB |
Output is correct |
67 |
Correct |
387 ms |
12928 KB |
Output is correct |
68 |
Correct |
383 ms |
11640 KB |
Output is correct |
69 |
Correct |
354 ms |
11640 KB |
Output is correct |
70 |
Correct |
371 ms |
11384 KB |
Output is correct |
71 |
Correct |
385 ms |
11260 KB |
Output is correct |
72 |
Correct |
367 ms |
10732 KB |
Output is correct |
73 |
Correct |
353 ms |
16504 KB |
Output is correct |
74 |
Correct |
365 ms |
15736 KB |
Output is correct |
75 |
Correct |
385 ms |
15864 KB |
Output is correct |
76 |
Correct |
377 ms |
15736 KB |
Output is correct |
77 |
Correct |
378 ms |
15512 KB |
Output is correct |
78 |
Correct |
406 ms |
16760 KB |
Output is correct |
79 |
Correct |
399 ms |
15324 KB |
Output is correct |
80 |
Correct |
384 ms |
15740 KB |
Output is correct |
81 |
Correct |
390 ms |
16120 KB |
Output is correct |
82 |
Correct |
380 ms |
16120 KB |
Output is correct |
83 |
Correct |
407 ms |
16672 KB |
Output is correct |
84 |
Correct |
383 ms |
16376 KB |
Output is correct |
85 |
Correct |
407 ms |
16652 KB |
Output is correct |
86 |
Correct |
393 ms |
16648 KB |
Output is correct |
87 |
Correct |
399 ms |
16688 KB |
Output is correct |
88 |
Correct |
411 ms |
17144 KB |
Output is correct |
89 |
Correct |
396 ms |
16948 KB |
Output is correct |
90 |
Correct |
393 ms |
16980 KB |
Output is correct |
91 |
Correct |
383 ms |
17148 KB |
Output is correct |
92 |
Correct |
406 ms |
16928 KB |
Output is correct |