#include <bits/stdc++.h>
#pragma GCC optimize ("Ofast")
#define SIZE 805
#define all(x) x.begin(), x.end()
#define debug(x) cout<<#x<<":"<<x<<endl;
using namespace std;
int n, mxstep;
char a[SIZE][SIZE];
bool vis[SIZE][SIZE];
bool bee[SIZE][SIZE];
struct node
{
int x, y;
int s;
bool operator ==(node b)
{
return x==b.x && y==b.y;
}
};
vector<node> hive;
node start;
node endpo;
int dx[]={-1, 1, 0, 0};
int dy[]={0, 0, -1, 1};
bool bfs(int wait)
{
queue<node> q; // bear
q.push(start);
queue<node> Q; // bee
memset(vis, 0, sizeof(vis));
memset(bee, 0, sizeof(bee));
vis[start.x][start.y]=1;
for(node t:hive)
{
bee[t.x][t.y]=1;
Q.push(t);
}
// 吃蜜时间蜜蜂围攻
while(!Q.empty() && Q.front().s<wait)
{
node w=Q.front(); Q.pop();
int i=w.x, j=w.y;
for(int k=0; k<4; k++)
{
int ni=i+dx[k];
int nj=j+dy[k];
// 不越界
if(ni<0 || ni>=n || nj<0 || nj>=n)
continue;
// 不走回头路
if(bee[ni][nj])
continue;
// 不入禁区
if(a[ni][nj]=='D' || a[ni][nj]=='T')
continue;
bee[ni][nj]=1;
Q.push({ni, nj, w.s+1});
}
}
// 原始位置已被攻占
if(bee[start.x][start.y])
return 0;
int steps=1; // 行走的次数
while(!q.empty())
{
// 小熊先走
while(!q.empty() && q.front().s<steps*mxstep)
{
node w=q.front(); q.pop();
int i=w.x, j=w.y;
if(w==endpo) return 1;
if(bee[i][j]) continue;
for(int k=0; k<4; k++)
{
int ni=i+dx[k];
int nj=j+dy[k];
// 不越界
if(ni<0 || ni>=n || nj<0 || nj>=n)
continue;
// 不被蜜蜂蛰
if(bee[ni][nj])
continue;
// 不走回头路
if(vis[ni][nj])
continue;
// 不入禁区
if(a[ni][nj]=='T')
continue;
vis[ni][nj]=1;
q.push({ni, nj, w.s+1});
}
}
// 蜜蜂后走
while(!Q.empty() && Q.front().s<wait+steps)
{
node w=Q.front(); Q.pop();
int i=w.x, j=w.y;
for(int k=0; k<4; k++)
{
int ni=i+dx[k];
int nj=j+dy[k];
// 不越界
if(ni<0 || ni>=n || nj<0 || nj>=n)
continue;
// 不走回头路
if(bee[ni][nj])
continue;
// 不入禁区
if(a[ni][nj]=='D' || a[ni][nj]=='T')
continue;
bee[ni][nj]=1;
Q.push({ni, nj, w.s+1});
}
}
steps++;
}
return 0;
}
signed main()
{
cin>>n>>mxstep;
int si, sj;
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
{
cin>>a[i][j];
if(a[i][j]=='H')
hive.push_back({i, j, 0});
if(a[i][j]=='M')
start={i, j, 0};
if(a[i][j]=='D')
endpo={i, j, 0};
}
int l=0, r=n*n;
int ans=-1;
while(l<=r)
{
int m=(l+r)>>1;
if(bfs(m))
{
ans=m;
l=m+1;
}
else
r=m-1;
}
cout<<ans;
return 0;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:126:6: warning: unused variable 'si' [-Wunused-variable]
126 | int si, sj;
| ^~
mecho.cpp:126:10: warning: unused variable 'sj' [-Wunused-variable]
126 | int si, sj;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1624 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1880 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
202 ms |
2484 KB |
Output is correct |
8 |
Correct |
1 ms |
1624 KB |
Output is correct |
9 |
Correct |
1 ms |
1624 KB |
Output is correct |
10 |
Correct |
1 ms |
1624 KB |
Output is correct |
11 |
Correct |
1 ms |
1628 KB |
Output is correct |
12 |
Correct |
2 ms |
1628 KB |
Output is correct |
13 |
Correct |
2 ms |
1628 KB |
Output is correct |
14 |
Correct |
2 ms |
1628 KB |
Output is correct |
15 |
Correct |
1 ms |
1624 KB |
Output is correct |
16 |
Correct |
1 ms |
1628 KB |
Output is correct |
17 |
Correct |
1 ms |
1628 KB |
Output is correct |
18 |
Correct |
2 ms |
1628 KB |
Output is correct |
19 |
Correct |
1 ms |
1628 KB |
Output is correct |
20 |
Correct |
1 ms |
1628 KB |
Output is correct |
21 |
Correct |
2 ms |
1628 KB |
Output is correct |
22 |
Correct |
2 ms |
1628 KB |
Output is correct |
23 |
Correct |
1 ms |
1628 KB |
Output is correct |
24 |
Correct |
1 ms |
1752 KB |
Output is correct |
25 |
Correct |
1 ms |
1624 KB |
Output is correct |
26 |
Correct |
2 ms |
1628 KB |
Output is correct |
27 |
Correct |
1 ms |
1628 KB |
Output is correct |
28 |
Correct |
2 ms |
1628 KB |
Output is correct |
29 |
Correct |
2 ms |
1628 KB |
Output is correct |
30 |
Correct |
2 ms |
1628 KB |
Output is correct |
31 |
Correct |
3 ms |
1628 KB |
Output is correct |
32 |
Correct |
2 ms |
1624 KB |
Output is correct |
33 |
Correct |
18 ms |
1884 KB |
Output is correct |
34 |
Correct |
17 ms |
1884 KB |
Output is correct |
35 |
Correct |
31 ms |
1884 KB |
Output is correct |
36 |
Correct |
25 ms |
2080 KB |
Output is correct |
37 |
Correct |
23 ms |
1884 KB |
Output is correct |
38 |
Correct |
32 ms |
2000 KB |
Output is correct |
39 |
Correct |
29 ms |
1884 KB |
Output is correct |
40 |
Correct |
28 ms |
1880 KB |
Output is correct |
41 |
Correct |
49 ms |
2064 KB |
Output is correct |
42 |
Correct |
36 ms |
1884 KB |
Output is correct |
43 |
Correct |
36 ms |
2072 KB |
Output is correct |
44 |
Correct |
52 ms |
1884 KB |
Output is correct |
45 |
Correct |
46 ms |
1884 KB |
Output is correct |
46 |
Correct |
42 ms |
2140 KB |
Output is correct |
47 |
Correct |
63 ms |
2140 KB |
Output is correct |
48 |
Correct |
55 ms |
2136 KB |
Output is correct |
49 |
Correct |
51 ms |
2140 KB |
Output is correct |
50 |
Correct |
73 ms |
2172 KB |
Output is correct |
51 |
Correct |
66 ms |
2140 KB |
Output is correct |
52 |
Correct |
61 ms |
2184 KB |
Output is correct |
53 |
Correct |
95 ms |
2200 KB |
Output is correct |
54 |
Correct |
79 ms |
2140 KB |
Output is correct |
55 |
Correct |
70 ms |
2204 KB |
Output is correct |
56 |
Correct |
106 ms |
2248 KB |
Output is correct |
57 |
Correct |
82 ms |
2140 KB |
Output is correct |
58 |
Correct |
80 ms |
2136 KB |
Output is correct |
59 |
Correct |
118 ms |
2140 KB |
Output is correct |
60 |
Correct |
98 ms |
2312 KB |
Output is correct |
61 |
Correct |
118 ms |
2312 KB |
Output is correct |
62 |
Correct |
139 ms |
2540 KB |
Output is correct |
63 |
Correct |
247 ms |
2140 KB |
Output is correct |
64 |
Correct |
271 ms |
2140 KB |
Output is correct |
65 |
Correct |
238 ms |
2136 KB |
Output is correct |
66 |
Correct |
248 ms |
2136 KB |
Output is correct |
67 |
Correct |
242 ms |
2392 KB |
Output is correct |
68 |
Correct |
261 ms |
2140 KB |
Output is correct |
69 |
Correct |
224 ms |
2140 KB |
Output is correct |
70 |
Correct |
240 ms |
2580 KB |
Output is correct |
71 |
Correct |
260 ms |
2136 KB |
Output is correct |
72 |
Correct |
282 ms |
2564 KB |
Output is correct |
73 |
Correct |
162 ms |
2712 KB |
Output is correct |
74 |
Correct |
200 ms |
2772 KB |
Output is correct |
75 |
Correct |
213 ms |
2652 KB |
Output is correct |
76 |
Correct |
208 ms |
2640 KB |
Output is correct |
77 |
Correct |
206 ms |
2652 KB |
Output is correct |
78 |
Correct |
214 ms |
2652 KB |
Output is correct |
79 |
Correct |
204 ms |
2732 KB |
Output is correct |
80 |
Correct |
209 ms |
2716 KB |
Output is correct |
81 |
Correct |
211 ms |
2712 KB |
Output is correct |
82 |
Correct |
206 ms |
2640 KB |
Output is correct |
83 |
Correct |
210 ms |
2652 KB |
Output is correct |
84 |
Correct |
211 ms |
2652 KB |
Output is correct |
85 |
Correct |
250 ms |
2688 KB |
Output is correct |
86 |
Correct |
215 ms |
2652 KB |
Output is correct |
87 |
Correct |
245 ms |
2648 KB |
Output is correct |
88 |
Correct |
201 ms |
2384 KB |
Output is correct |
89 |
Correct |
209 ms |
2572 KB |
Output is correct |
90 |
Correct |
231 ms |
2568 KB |
Output is correct |
91 |
Correct |
211 ms |
2384 KB |
Output is correct |
92 |
Correct |
212 ms |
2568 KB |
Output is correct |