#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 |
Incorrect |
2 ms |
4696 KB |
Output isn't correct |
2 |
Incorrect |
2 ms |
4444 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
7 |
Incorrect |
296 ms |
6864 KB |
Output isn't correct |
8 |
Incorrect |
2 ms |
4444 KB |
Output isn't correct |
9 |
Correct |
1 ms |
4444 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 |
4700 KB |
Output isn't correct |
14 |
Correct |
2 ms |
4700 KB |
Output is correct |
15 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Incorrect |
1 ms |
4696 KB |
Output isn't correct |
18 |
Correct |
1 ms |
4440 KB |
Output is correct |
19 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
24 |
Correct |
1 ms |
4700 KB |
Output is correct |
25 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
26 |
Correct |
1 ms |
4700 KB |
Output is correct |
27 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
28 |
Correct |
1 ms |
4700 KB |
Output is correct |
29 |
Incorrect |
1 ms |
4696 KB |
Output isn't correct |
30 |
Correct |
1 ms |
4700 KB |
Output is correct |
31 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
32 |
Correct |
1 ms |
4700 KB |
Output is correct |
33 |
Incorrect |
8 ms |
5468 KB |
Output isn't correct |
34 |
Correct |
8 ms |
5468 KB |
Output is correct |
35 |
Correct |
105 ms |
5652 KB |
Output is correct |
36 |
Incorrect |
8 ms |
5724 KB |
Output isn't correct |
37 |
Correct |
9 ms |
5724 KB |
Output is correct |
38 |
Correct |
142 ms |
5804 KB |
Output is correct |
39 |
Incorrect |
11 ms |
5976 KB |
Output isn't correct |
40 |
Correct |
14 ms |
5976 KB |
Output is correct |
41 |
Correct |
185 ms |
5980 KB |
Output is correct |
42 |
Incorrect |
11 ms |
5980 KB |
Output isn't correct |
43 |
Correct |
18 ms |
6108 KB |
Output is correct |
44 |
Correct |
222 ms |
6372 KB |
Output is correct |
45 |
Incorrect |
15 ms |
6232 KB |
Output isn't correct |
46 |
Correct |
18 ms |
6304 KB |
Output is correct |
47 |
Correct |
282 ms |
6236 KB |
Output is correct |
48 |
Incorrect |
18 ms |
6232 KB |
Output isn't correct |
49 |
Correct |
22 ms |
6236 KB |
Output is correct |
50 |
Correct |
326 ms |
6444 KB |
Output is correct |
51 |
Incorrect |
21 ms |
6492 KB |
Output isn't correct |
52 |
Correct |
21 ms |
6492 KB |
Output is correct |
53 |
Correct |
411 ms |
6600 KB |
Output is correct |
54 |
Incorrect |
28 ms |
7004 KB |
Output isn't correct |
55 |
Correct |
28 ms |
6612 KB |
Output is correct |
56 |
Correct |
466 ms |
6736 KB |
Output is correct |
57 |
Incorrect |
25 ms |
6488 KB |
Output isn't correct |
58 |
Correct |
31 ms |
6748 KB |
Output is correct |
59 |
Correct |
629 ms |
6780 KB |
Output is correct |
60 |
Incorrect |
28 ms |
6748 KB |
Output isn't correct |
61 |
Correct |
29 ms |
6604 KB |
Output is correct |
62 |
Correct |
525 ms |
6740 KB |
Output is correct |
63 |
Correct |
185 ms |
6736 KB |
Output is correct |
64 |
Correct |
566 ms |
6784 KB |
Output is correct |
65 |
Correct |
358 ms |
6788 KB |
Output is correct |
66 |
Incorrect |
209 ms |
6784 KB |
Output isn't correct |
67 |
Correct |
166 ms |
6740 KB |
Output is correct |
68 |
Correct |
108 ms |
6808 KB |
Output is correct |
69 |
Correct |
76 ms |
6652 KB |
Output is correct |
70 |
Correct |
67 ms |
6636 KB |
Output is correct |
71 |
Correct |
55 ms |
6816 KB |
Output is correct |
72 |
Incorrect |
47 ms |
6748 KB |
Output isn't correct |
73 |
Incorrect |
91 ms |
7104 KB |
Output isn't correct |
74 |
Correct |
232 ms |
7200 KB |
Output is correct |
75 |
Correct |
187 ms |
6940 KB |
Output is correct |
76 |
Correct |
176 ms |
6920 KB |
Output is correct |
77 |
Correct |
193 ms |
6872 KB |
Output is correct |
78 |
Correct |
183 ms |
6992 KB |
Output is correct |
79 |
Correct |
193 ms |
7140 KB |
Output is correct |
80 |
Correct |
178 ms |
7064 KB |
Output is correct |
81 |
Correct |
219 ms |
7064 KB |
Output is correct |
82 |
Correct |
215 ms |
7004 KB |
Output is correct |
83 |
Correct |
219 ms |
6996 KB |
Output is correct |
84 |
Correct |
303 ms |
7020 KB |
Output is correct |
85 |
Correct |
270 ms |
6996 KB |
Output is correct |
86 |
Correct |
282 ms |
7020 KB |
Output is correct |
87 |
Correct |
287 ms |
7016 KB |
Output is correct |
88 |
Correct |
275 ms |
6956 KB |
Output is correct |
89 |
Correct |
444 ms |
6952 KB |
Output is correct |
90 |
Correct |
276 ms |
6796 KB |
Output is correct |
91 |
Correct |
330 ms |
7036 KB |
Output is correct |
92 |
Correct |
303 ms |
6956 KB |
Output is correct |