#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define pb push_back
#define mp make_pair
#define f first
#define s second
int n, s;
pi st, en;
char a[805][805];
bool visited[805][805];
int dist[805][805];
int dist2[805][805];
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
bool is_possible(pi temp, int type, int time){
if(temp.f <= 0 || temp.s <= 0 || temp.f > n || temp.s > n)
return false;
else if(visited[temp.f][temp.s] || a[temp.f][temp.s] == 'T' || a[temp.f][temp.s] == 'H')
return false;
if(type && dist[temp.f][temp.s] <= time)
return false;
return true;
}
void bfs(vector<pi> b, int type, int time){ //time = 0 if bees
queue<pi> q;
for(auto start : b){
q.push(start);
visited[start.f][start.s] = 1;
if(!type)
dist[start.f][start.s] = 0;
else
dist2[start.f][start.s] = time;
}
while(!q.empty()){
pi current = q.front();
q.pop();
for(int i = 0; i < 4; i++){
pi temp = {current.f + dx[i], current.s + dy[i]};
if(is_possible(temp, type, dist2[current.f][current.s] + 1)){
q.push(temp);
visited[temp.f][temp.s] = 1;
if(!type)
dist[temp.f][temp.s] = dist[current.f][current.s] + 1;
else
dist2[temp.f][temp.s] = dist2[current.f][current.s] + 1;
}
}
}
}
void clear(){
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
dist2[i][j] = 0;
visited[i][j] = 0;
}
}
}
void solve(){
cin >> n >> s;
vector<pi> bees;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
cin >> a[i][j];
if(a[i][j] == 'H')
bees.pb({i, j});
if(a[i][j] == 'M')
st = {i, j};
if(a[i][j] == 'D')
en = {i, j};
}
}
bfs(bees, 0, 0);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
dist[i][j] = s * dist[i][j]; //1 step for bees = s for bear
if(!visited[st.f][st.s]){ //bees cant reach bear
cout << -1 << '\n';
return;
}
//binary search for how much time to eat honey
int current = -1;
for(int i = 1e9; i > 0; i /= 2){
while(true){
clear();
bfs({st}, 1, current+i);
if(visited[en.f][en.s])
current += i;
else
break;
}
}
if(current == -1){ //bear cant reach home
cout << -1 << '\n';
return;
}
cout << current / s << '\n';
}
int main(){
//freopen("infasuratoare.in", "r", stdin);
//freopen("infasuratoare.out", "w", stdout);
ios::sync_with_stdio(0); cin.tie(0);
int t = 1;
//cin >> t;
while(t--){
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
7 |
Incorrect |
309 ms |
6900 KB |
Output isn't correct |
8 |
Correct |
1 ms |
4696 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
4564 KB |
Output isn't correct |
14 |
Correct |
1 ms |
4700 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4440 KB |
Output is correct |
17 |
Correct |
1 ms |
4440 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4440 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
4700 KB |
Output is correct |
24 |
Correct |
1 ms |
4700 KB |
Output is correct |
25 |
Correct |
1 ms |
4700 KB |
Output is correct |
26 |
Correct |
1 ms |
4700 KB |
Output is correct |
27 |
Correct |
1 ms |
4700 KB |
Output is correct |
28 |
Correct |
1 ms |
4700 KB |
Output is correct |
29 |
Correct |
2 ms |
4700 KB |
Output is correct |
30 |
Correct |
2 ms |
4700 KB |
Output is correct |
31 |
Correct |
1 ms |
4700 KB |
Output is correct |
32 |
Correct |
1 ms |
4700 KB |
Output is correct |
33 |
Correct |
8 ms |
5464 KB |
Output is correct |
34 |
Correct |
8 ms |
5468 KB |
Output is correct |
35 |
Correct |
102 ms |
5468 KB |
Output is correct |
36 |
Correct |
8 ms |
5724 KB |
Output is correct |
37 |
Correct |
8 ms |
5724 KB |
Output is correct |
38 |
Correct |
140 ms |
5808 KB |
Output is correct |
39 |
Correct |
13 ms |
5976 KB |
Output is correct |
40 |
Correct |
12 ms |
5976 KB |
Output is correct |
41 |
Correct |
172 ms |
5964 KB |
Output is correct |
42 |
Correct |
11 ms |
5976 KB |
Output is correct |
43 |
Correct |
18 ms |
5980 KB |
Output is correct |
44 |
Correct |
231 ms |
5976 KB |
Output is correct |
45 |
Correct |
15 ms |
6104 KB |
Output is correct |
46 |
Correct |
17 ms |
6104 KB |
Output is correct |
47 |
Correct |
259 ms |
6236 KB |
Output is correct |
48 |
Correct |
18 ms |
6236 KB |
Output is correct |
49 |
Correct |
21 ms |
6236 KB |
Output is correct |
50 |
Correct |
345 ms |
6444 KB |
Output is correct |
51 |
Correct |
22 ms |
6492 KB |
Output is correct |
52 |
Correct |
21 ms |
6488 KB |
Output is correct |
53 |
Correct |
395 ms |
6596 KB |
Output is correct |
54 |
Correct |
27 ms |
6748 KB |
Output is correct |
55 |
Correct |
28 ms |
6616 KB |
Output is correct |
56 |
Correct |
454 ms |
6756 KB |
Output is correct |
57 |
Correct |
27 ms |
6532 KB |
Output is correct |
58 |
Correct |
32 ms |
6744 KB |
Output is correct |
59 |
Correct |
613 ms |
6780 KB |
Output is correct |
60 |
Correct |
28 ms |
6740 KB |
Output is correct |
61 |
Correct |
26 ms |
6744 KB |
Output is correct |
62 |
Correct |
556 ms |
6780 KB |
Output is correct |
63 |
Correct |
161 ms |
6740 KB |
Output is correct |
64 |
Correct |
531 ms |
6788 KB |
Output is correct |
65 |
Correct |
380 ms |
6992 KB |
Output is correct |
66 |
Correct |
242 ms |
6784 KB |
Output is correct |
67 |
Correct |
168 ms |
6784 KB |
Output is correct |
68 |
Correct |
102 ms |
6736 KB |
Output is correct |
69 |
Correct |
81 ms |
6740 KB |
Output is correct |
70 |
Correct |
61 ms |
6656 KB |
Output is correct |
71 |
Correct |
60 ms |
6656 KB |
Output is correct |
72 |
Incorrect |
46 ms |
6652 KB |
Output isn't correct |
73 |
Incorrect |
103 ms |
6856 KB |
Output isn't correct |
74 |
Correct |
220 ms |
7100 KB |
Output is correct |
75 |
Correct |
197 ms |
6992 KB |
Output is correct |
76 |
Correct |
183 ms |
7108 KB |
Output is correct |
77 |
Correct |
182 ms |
6924 KB |
Output is correct |
78 |
Correct |
194 ms |
7064 KB |
Output is correct |
79 |
Correct |
197 ms |
6900 KB |
Output is correct |
80 |
Correct |
187 ms |
7068 KB |
Output is correct |
81 |
Correct |
235 ms |
7064 KB |
Output is correct |
82 |
Correct |
206 ms |
7064 KB |
Output is correct |
83 |
Correct |
203 ms |
7020 KB |
Output is correct |
84 |
Correct |
338 ms |
6852 KB |
Output is correct |
85 |
Correct |
264 ms |
6856 KB |
Output is correct |
86 |
Correct |
265 ms |
7020 KB |
Output is correct |
87 |
Correct |
299 ms |
6996 KB |
Output is correct |
88 |
Correct |
256 ms |
6740 KB |
Output is correct |
89 |
Correct |
422 ms |
6740 KB |
Output is correct |
90 |
Correct |
301 ms |
6960 KB |
Output is correct |
91 |
Correct |
319 ms |
6952 KB |
Output is correct |
92 |
Correct |
292 ms |
6740 KB |
Output is correct |