#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
struct Node{
int x,y,tm;
};
int n,s;
int vis[805][805];
int _map[805][805];
int map[805][805];
int side[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
queue<Node> _bee;
queue<Node> bee;
void print()
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)cout<<map[i][j];
cout<<endl;
}
cout<<endl;
}
void reset()
{
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
map[i][j]=_map[i][j];
bee=_bee;
memset(vis,0,sizeof(vis));
}
void runbee(int t)
{
if(t==0)return;
int btm=bee.front().tm;
while(!bee.empty())
{
Node res=bee.front();
int x=res.x;
int y=res.y;
int tm=res.tm;
if(res.tm==btm+t)break;
bee.pop();
//cout<<x<<" "<<y<<endl;
for(int i=0;i<4;i++)
{
int newx=x+side[i][0];
int newy=y+side[i][1];
//cout<<" "<<newx<<" "<<newy<<endl;
if(newx<0 || newy<0 || newx>=n || newy>=n)continue;
if(map[newx][newy]==0 || map[newx][newy]==2)
{
bee.push({newx,newy,tm+1});
map[newx][newy]=3;
}
}
}
}
bool bfs(int sx,int sy,int t)
{
reset();
runbee(t);
queue<Node> que;
if(map[sx][sy]==3)return 0;
que.push({sx,sy,0});
vis[sx][sy]=1;
int nowt=0;
int x,y;
while(!que.empty())
{
Node res=que.front();
que.pop();
x=res.x;
y=res.y;
int tm=res.tm;
if(res.tm>nowt && res.tm%s==0)runbee(1);
nowt=res.tm;
if(map[x][y]==3)continue;
if(map[x][y]==4)return 1;
for(int i=0;i<4;i++)
{
int newx=x+side[i][0];
int newy=y+side[i][1];
if(newx<0 || newy<0 || newx>=n || newy>=n)continue;
if(map[newx][newy]==3 || map[newx][newy]==1)continue;
if(vis[newx][newy])continue;
que.push({newx,newy,tm+1});
vis[newx][newy]=1;
}
}
return 0;
}
int main()
{
//freopen("./data/mecho.092.in","rb",stdin);
cin>>n>>s;
int sx,sy;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
char res;
cin>>res;
if(res=='G')_map[i][j]=0;
else if(res=='T')_map[i][j]=1;
else if(res=='M')_map[i][j]=2,sx=i,sy=j;
else if(res=='H')_map[i][j]=3,_bee.push({i,j,0});
else if(res=='D')_map[i][j]=4;
}
}
if(!bfs(sx,sy,0))cout<<"-1"<<endl;
else
{
if(!bfs(sx,sy,1))cout<<"0"<<endl;
else
{
int left=1,right=1;
while(bfs(sx,sy,right))left=right,right*=2;
if(right==1)cout<<"1"<<endl;
else
{
right--;
while(left<right)
{
int mid=(left+right)/2;
if((left+right)%2==1)mid++;
if(bfs(sx,sy,mid))left=mid;
else right=mid-1;
}
cout<<left<<endl;
}
}
}
return 0;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:118:10: warning: 'sy' may be used uninitialized in this function [-Wmaybe-uninitialized]
if(!bfs(sx,sy,1))cout<<"0"<<endl;
~~~^~~~~~~~~
mecho.cpp:118:10: warning: 'sx' may be used uninitialized in this function [-Wmaybe-uninitialized]
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
2936 KB |
Output is correct |
2 |
Correct |
5 ms |
2936 KB |
Output is correct |
3 |
Correct |
5 ms |
2936 KB |
Output is correct |
4 |
Correct |
5 ms |
2936 KB |
Output is correct |
5 |
Correct |
4 ms |
2936 KB |
Output is correct |
6 |
Correct |
5 ms |
2936 KB |
Output is correct |
7 |
Correct |
324 ms |
8752 KB |
Output is correct |
8 |
Correct |
5 ms |
2936 KB |
Output is correct |
9 |
Correct |
7 ms |
2940 KB |
Output is correct |
10 |
Correct |
5 ms |
2936 KB |
Output is correct |
11 |
Correct |
5 ms |
2936 KB |
Output is correct |
12 |
Correct |
7 ms |
3192 KB |
Output is correct |
13 |
Correct |
6 ms |
3192 KB |
Output is correct |
14 |
Correct |
6 ms |
3192 KB |
Output is correct |
15 |
Correct |
8 ms |
3064 KB |
Output is correct |
16 |
Correct |
7 ms |
2964 KB |
Output is correct |
17 |
Correct |
8 ms |
3064 KB |
Output is correct |
18 |
Correct |
8 ms |
3064 KB |
Output is correct |
19 |
Correct |
7 ms |
3192 KB |
Output is correct |
20 |
Correct |
8 ms |
3064 KB |
Output is correct |
21 |
Correct |
8 ms |
3064 KB |
Output is correct |
22 |
Correct |
8 ms |
3064 KB |
Output is correct |
23 |
Correct |
9 ms |
3192 KB |
Output is correct |
24 |
Correct |
19 ms |
3192 KB |
Output is correct |
25 |
Correct |
9 ms |
3320 KB |
Output is correct |
26 |
Correct |
9 ms |
3192 KB |
Output is correct |
27 |
Correct |
10 ms |
3320 KB |
Output is correct |
28 |
Correct |
9 ms |
3320 KB |
Output is correct |
29 |
Correct |
11 ms |
3320 KB |
Output is correct |
30 |
Correct |
17 ms |
3320 KB |
Output is correct |
31 |
Correct |
11 ms |
3320 KB |
Output is correct |
32 |
Correct |
10 ms |
3320 KB |
Output is correct |
33 |
Correct |
60 ms |
5112 KB |
Output is correct |
34 |
Correct |
49 ms |
5232 KB |
Output is correct |
35 |
Correct |
35 ms |
5240 KB |
Output is correct |
36 |
Correct |
61 ms |
5584 KB |
Output is correct |
37 |
Correct |
61 ms |
5496 KB |
Output is correct |
38 |
Correct |
43 ms |
5592 KB |
Output is correct |
39 |
Correct |
72 ms |
5880 KB |
Output is correct |
40 |
Correct |
70 ms |
5880 KB |
Output is correct |
41 |
Correct |
52 ms |
5880 KB |
Output is correct |
42 |
Correct |
94 ms |
6436 KB |
Output is correct |
43 |
Correct |
88 ms |
6264 KB |
Output is correct |
44 |
Correct |
68 ms |
6264 KB |
Output is correct |
45 |
Correct |
154 ms |
6668 KB |
Output is correct |
46 |
Correct |
111 ms |
6648 KB |
Output is correct |
47 |
Correct |
100 ms |
6748 KB |
Output is correct |
48 |
Correct |
143 ms |
7132 KB |
Output is correct |
49 |
Correct |
144 ms |
7048 KB |
Output is correct |
50 |
Correct |
98 ms |
7032 KB |
Output is correct |
51 |
Correct |
165 ms |
7416 KB |
Output is correct |
52 |
Correct |
165 ms |
7468 KB |
Output is correct |
53 |
Correct |
127 ms |
7544 KB |
Output is correct |
54 |
Correct |
208 ms |
7928 KB |
Output is correct |
55 |
Correct |
184 ms |
7840 KB |
Output is correct |
56 |
Correct |
138 ms |
7840 KB |
Output is correct |
57 |
Correct |
210 ms |
8248 KB |
Output is correct |
58 |
Correct |
198 ms |
8312 KB |
Output is correct |
59 |
Correct |
142 ms |
8184 KB |
Output is correct |
60 |
Correct |
256 ms |
8572 KB |
Output is correct |
61 |
Correct |
265 ms |
8696 KB |
Output is correct |
62 |
Correct |
162 ms |
8696 KB |
Output is correct |
63 |
Correct |
119 ms |
8568 KB |
Output is correct |
64 |
Correct |
520 ms |
8584 KB |
Output is correct |
65 |
Correct |
468 ms |
8696 KB |
Output is correct |
66 |
Correct |
478 ms |
8576 KB |
Output is correct |
67 |
Correct |
90 ms |
8572 KB |
Output is correct |
68 |
Correct |
105 ms |
8720 KB |
Output is correct |
69 |
Correct |
160 ms |
8568 KB |
Output is correct |
70 |
Correct |
261 ms |
8664 KB |
Output is correct |
71 |
Correct |
231 ms |
8768 KB |
Output is correct |
72 |
Correct |
308 ms |
8628 KB |
Output is correct |
73 |
Correct |
132 ms |
9080 KB |
Output is correct |
74 |
Correct |
391 ms |
9036 KB |
Output is correct |
75 |
Correct |
417 ms |
8952 KB |
Output is correct |
76 |
Correct |
362 ms |
9080 KB |
Output is correct |
77 |
Correct |
448 ms |
9080 KB |
Output is correct |
78 |
Correct |
101 ms |
8952 KB |
Output is correct |
79 |
Correct |
371 ms |
9084 KB |
Output is correct |
80 |
Correct |
371 ms |
8952 KB |
Output is correct |
81 |
Correct |
333 ms |
9028 KB |
Output is correct |
82 |
Correct |
365 ms |
8952 KB |
Output is correct |
83 |
Correct |
112 ms |
8824 KB |
Output is correct |
84 |
Correct |
357 ms |
8952 KB |
Output is correct |
85 |
Correct |
347 ms |
8904 KB |
Output is correct |
86 |
Correct |
320 ms |
8952 KB |
Output is correct |
87 |
Correct |
382 ms |
8952 KB |
Output is correct |
88 |
Correct |
122 ms |
8828 KB |
Output is correct |
89 |
Correct |
337 ms |
8796 KB |
Output is correct |
90 |
Correct |
348 ms |
8836 KB |
Output is correct |
91 |
Correct |
370 ms |
8968 KB |
Output is correct |
92 |
Correct |
286 ms |
8924 KB |
Output is correct |