#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)
{
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*n,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++;
}
//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 |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
408 ms |
16988 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
504 KB |
Output is correct |
11 |
Correct |
3 ms |
376 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 |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
504 KB |
Output is correct |
17 |
Correct |
2 ms |
508 KB |
Output is correct |
18 |
Correct |
2 ms |
508 KB |
Output is correct |
19 |
Correct |
2 ms |
632 KB |
Output is correct |
20 |
Correct |
2 ms |
504 KB |
Output is correct |
21 |
Correct |
3 ms |
632 KB |
Output is correct |
22 |
Correct |
3 ms |
632 KB |
Output is correct |
23 |
Correct |
3 ms |
760 KB |
Output is correct |
24 |
Correct |
3 ms |
760 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 |
760 KB |
Output is correct |
28 |
Correct |
3 ms |
764 KB |
Output is correct |
29 |
Correct |
3 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 |
41 ms |
3448 KB |
Output is correct |
34 |
Correct |
42 ms |
3448 KB |
Output is correct |
35 |
Correct |
67 ms |
4856 KB |
Output is correct |
36 |
Correct |
54 ms |
4216 KB |
Output is correct |
37 |
Correct |
59 ms |
4104 KB |
Output is correct |
38 |
Correct |
98 ms |
5928 KB |
Output is correct |
39 |
Correct |
68 ms |
4600 KB |
Output is correct |
40 |
Correct |
77 ms |
4728 KB |
Output is correct |
41 |
Correct |
116 ms |
7004 KB |
Output is correct |
42 |
Correct |
86 ms |
5240 KB |
Output is correct |
43 |
Correct |
86 ms |
5496 KB |
Output is correct |
44 |
Correct |
145 ms |
8200 KB |
Output is correct |
45 |
Correct |
106 ms |
5960 KB |
Output is correct |
46 |
Correct |
111 ms |
6108 KB |
Output is correct |
47 |
Correct |
169 ms |
9452 KB |
Output is correct |
48 |
Correct |
120 ms |
6628 KB |
Output is correct |
49 |
Correct |
129 ms |
6672 KB |
Output is correct |
50 |
Correct |
209 ms |
10744 KB |
Output is correct |
51 |
Correct |
150 ms |
7388 KB |
Output is correct |
52 |
Correct |
152 ms |
7288 KB |
Output is correct |
53 |
Correct |
242 ms |
12300 KB |
Output is correct |
54 |
Correct |
179 ms |
8156 KB |
Output is correct |
55 |
Correct |
176 ms |
8056 KB |
Output is correct |
56 |
Correct |
295 ms |
13856 KB |
Output is correct |
57 |
Correct |
191 ms |
8824 KB |
Output is correct |
58 |
Correct |
208 ms |
8976 KB |
Output is correct |
59 |
Correct |
344 ms |
15396 KB |
Output is correct |
60 |
Correct |
237 ms |
9788 KB |
Output is correct |
61 |
Correct |
226 ms |
9592 KB |
Output is correct |
62 |
Correct |
380 ms |
17116 KB |
Output is correct |
63 |
Correct |
494 ms |
14200 KB |
Output is correct |
64 |
Correct |
496 ms |
14536 KB |
Output is correct |
65 |
Correct |
459 ms |
14456 KB |
Output is correct |
66 |
Correct |
452 ms |
13560 KB |
Output is correct |
67 |
Correct |
477 ms |
12668 KB |
Output is correct |
68 |
Correct |
418 ms |
11512 KB |
Output is correct |
69 |
Correct |
391 ms |
11640 KB |
Output is correct |
70 |
Correct |
411 ms |
11128 KB |
Output is correct |
71 |
Correct |
430 ms |
11100 KB |
Output is correct |
72 |
Correct |
390 ms |
10488 KB |
Output is correct |
73 |
Correct |
395 ms |
16356 KB |
Output is correct |
74 |
Correct |
396 ms |
15428 KB |
Output is correct |
75 |
Correct |
418 ms |
15900 KB |
Output is correct |
76 |
Correct |
500 ms |
15768 KB |
Output is correct |
77 |
Correct |
416 ms |
15632 KB |
Output is correct |
78 |
Correct |
446 ms |
16640 KB |
Output is correct |
79 |
Correct |
412 ms |
15224 KB |
Output is correct |
80 |
Correct |
414 ms |
15448 KB |
Output is correct |
81 |
Correct |
430 ms |
16084 KB |
Output is correct |
82 |
Correct |
407 ms |
15864 KB |
Output is correct |
83 |
Correct |
440 ms |
16528 KB |
Output is correct |
84 |
Correct |
408 ms |
16248 KB |
Output is correct |
85 |
Correct |
426 ms |
16504 KB |
Output is correct |
86 |
Correct |
426 ms |
16540 KB |
Output is correct |
87 |
Correct |
409 ms |
16632 KB |
Output is correct |
88 |
Correct |
439 ms |
16984 KB |
Output is correct |
89 |
Correct |
423 ms |
16936 KB |
Output is correct |
90 |
Correct |
423 ms |
16832 KB |
Output is correct |
91 |
Correct |
405 ms |
17020 KB |
Output is correct |
92 |
Correct |
444 ms |
17080 KB |
Output is correct |