// Mecho
// https://oj.uz/problem/view/IOI09_mecho
#include <stdio.h>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <array>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <algorithm>
#include <stdlib.h>
#include <math.h>
#include <list>
#include <float.h>
#include <climits>
#include <cmath>
using namespace std;
void quit() {
cout.flush();
exit(0);
}
int n, s;
pair<int, int> st, dest;
bool tree[800][800]; //contains trees and grass. bee hives are also trees
bool tmp[800][800];
bool mecho_done[800][800];
deque<pair<int, int>> hives, mecho, tmp1, tmp2, new_hives, new_mecho;
pair<int, int> gto[4] = {{0,1}, {0,-1}, {-1,0}, {1,0}};
bool in_grid(int a, int b){
return a >= 0 && a < n && b >= 0 && b < n;
}
bool bee_can_go(int a, int b){
return in_grid(a,b) && !(a == dest.first && b == dest.second) && !tree[a][b];
}
bool mecho_can_go(int a, int b){
return in_grid(a,b) && !mecho_done[a][b] && !tree[a][b];
}
void bee_expand(){
new_hives.clear();
while(!hives.empty()){
pair<int, int> curr = hives.front();
int cx = curr.first, cy = curr.second;
hives.pop_front();
for(const auto &to : gto){
int nx = to.first +cx, ny = to.second+cy;
if(bee_can_go(nx, ny)){
new_hives.emplace_back(nx, ny);
tree[nx][ny]=1;
}
}
}
hives=new_hives;
}
bool finished;
bool mecho_expand(){
//if can't expand anymore return false
for(int i = 0; i < s; i++){
new_mecho.clear();
while(!mecho.empty()){
pair<int, int> curr = mecho.front();
int cx = curr.first, cy = curr.second;
mecho.pop_front();
if(tree[cx][cy]){
continue;
}
for(const auto &to : gto){
int nx = to.first +cx, ny = to.second+cy;
if(mecho_can_go(nx, ny)){
new_mecho.emplace_back(nx, ny);
mecho_done[nx][ny]=1;
}
if(nx == dest.first && ny == dest.second){
//can get to dest
finished=true;
return false;
}
}
}
mecho=new_mecho;
}
return mecho.size()>0;
}
bool good(int wait_time){
tmp1 = hives;
tmp2 = mecho;
for(int i = 0; i < 800; i++){
for(int j = 0; j < 800; j++){
mecho_done[i][j]=0;
tmp[i][j]=tree[i][j];
}
}
mecho_done[st.first][st.second]=1;
finished=false;
//propogate until can't
for(int i = 0; i < wait_time; i++){
bee_expand();
}
while(mecho_expand()){
bee_expand();
/*
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
cerr <<tree[i][j] << " ";
}
cerr << "\n";
}
*/
}
//reset variables
for(int i = 0; i < 800; i++){
for(int j = 0; j < 800; j++){
tree[i][j]=tmp[i][j];
}
}
hives=tmp1;
mecho=tmp2;
return finished;
}
int main(void){
//freopen("qwer.in", "r", stdin);
//freopen("qwer.out", "w", stdout);
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> s;
for(int i = 0; i < n; i++){
string s; cin >> s;
for(int j = 0; j < n; j++){
if(s[j]=='T'){
tree[i][j]=1;
}
else if(s[j]=='M'){
st={i,j};
mecho.emplace_back(i,j);
}
else if(s[j]=='D'){
dest = {i,j};
}
else if(s[j] == 'H'){
hives.emplace_back(i,j);
tree[i][j]=1;
}
}
}
if(!good(0)){
cout << "-1\n";
quit();
}
int s = 0, e = 1e6;
while(s != e){
int md = (s+e+1)/2;
if(good(md)){
s=md;
}
else{
e=md-1;
}
}
cout << s << "\n";
quit();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
2132 KB |
Output is correct |
2 |
Correct |
28 ms |
2196 KB |
Output is correct |
3 |
Correct |
27 ms |
2196 KB |
Output is correct |
4 |
Correct |
26 ms |
2200 KB |
Output is correct |
5 |
Correct |
26 ms |
2132 KB |
Output is correct |
6 |
Correct |
28 ms |
2132 KB |
Output is correct |
7 |
Correct |
367 ms |
2436 KB |
Output is correct |
8 |
Correct |
1 ms |
2132 KB |
Output is correct |
9 |
Correct |
27 ms |
2200 KB |
Output is correct |
10 |
Correct |
30 ms |
2132 KB |
Output is correct |
11 |
Correct |
26 ms |
2132 KB |
Output is correct |
12 |
Correct |
27 ms |
2204 KB |
Output is correct |
13 |
Correct |
26 ms |
2132 KB |
Output is correct |
14 |
Correct |
1 ms |
2132 KB |
Output is correct |
15 |
Correct |
27 ms |
2132 KB |
Output is correct |
16 |
Correct |
28 ms |
2132 KB |
Output is correct |
17 |
Correct |
29 ms |
2132 KB |
Output is correct |
18 |
Correct |
25 ms |
2132 KB |
Output is correct |
19 |
Correct |
29 ms |
2188 KB |
Output is correct |
20 |
Correct |
26 ms |
2132 KB |
Output is correct |
21 |
Correct |
31 ms |
2180 KB |
Output is correct |
22 |
Correct |
34 ms |
2184 KB |
Output is correct |
23 |
Correct |
29 ms |
2132 KB |
Output is correct |
24 |
Correct |
30 ms |
2204 KB |
Output is correct |
25 |
Correct |
38 ms |
2132 KB |
Output is correct |
26 |
Correct |
30 ms |
2208 KB |
Output is correct |
27 |
Correct |
26 ms |
2204 KB |
Output is correct |
28 |
Correct |
29 ms |
2132 KB |
Output is correct |
29 |
Correct |
28 ms |
2180 KB |
Output is correct |
30 |
Correct |
26 ms |
2200 KB |
Output is correct |
31 |
Correct |
32 ms |
2132 KB |
Output is correct |
32 |
Correct |
30 ms |
2200 KB |
Output is correct |
33 |
Correct |
67 ms |
2132 KB |
Output is correct |
34 |
Correct |
67 ms |
2132 KB |
Output is correct |
35 |
Correct |
65 ms |
2132 KB |
Output is correct |
36 |
Correct |
94 ms |
2188 KB |
Output is correct |
37 |
Correct |
94 ms |
2192 KB |
Output is correct |
38 |
Correct |
79 ms |
2132 KB |
Output is correct |
39 |
Correct |
125 ms |
2208 KB |
Output is correct |
40 |
Correct |
100 ms |
2200 KB |
Output is correct |
41 |
Correct |
89 ms |
2208 KB |
Output is correct |
42 |
Correct |
149 ms |
2200 KB |
Output is correct |
43 |
Correct |
108 ms |
2132 KB |
Output is correct |
44 |
Correct |
105 ms |
2208 KB |
Output is correct |
45 |
Correct |
138 ms |
2200 KB |
Output is correct |
46 |
Correct |
144 ms |
2200 KB |
Output is correct |
47 |
Correct |
123 ms |
2236 KB |
Output is correct |
48 |
Correct |
219 ms |
2132 KB |
Output is correct |
49 |
Correct |
155 ms |
2204 KB |
Output is correct |
50 |
Correct |
140 ms |
2224 KB |
Output is correct |
51 |
Correct |
192 ms |
2212 KB |
Output is correct |
52 |
Correct |
191 ms |
2212 KB |
Output is correct |
53 |
Correct |
168 ms |
2224 KB |
Output is correct |
54 |
Correct |
228 ms |
2132 KB |
Output is correct |
55 |
Correct |
193 ms |
2204 KB |
Output is correct |
56 |
Correct |
184 ms |
2252 KB |
Output is correct |
57 |
Correct |
283 ms |
2208 KB |
Output is correct |
58 |
Correct |
228 ms |
2132 KB |
Output is correct |
59 |
Correct |
205 ms |
2228 KB |
Output is correct |
60 |
Correct |
356 ms |
2204 KB |
Output is correct |
61 |
Correct |
277 ms |
2208 KB |
Output is correct |
62 |
Correct |
231 ms |
2244 KB |
Output is correct |
63 |
Correct |
272 ms |
2132 KB |
Output is correct |
64 |
Correct |
300 ms |
2228 KB |
Output is correct |
65 |
Correct |
377 ms |
2216 KB |
Output is correct |
66 |
Correct |
337 ms |
2232 KB |
Output is correct |
67 |
Correct |
23 ms |
2152 KB |
Output is correct |
68 |
Correct |
294 ms |
2276 KB |
Output is correct |
69 |
Correct |
253 ms |
2280 KB |
Output is correct |
70 |
Correct |
279 ms |
2260 KB |
Output is correct |
71 |
Correct |
250 ms |
2276 KB |
Output is correct |
72 |
Correct |
354 ms |
2260 KB |
Output is correct |
73 |
Correct |
284 ms |
2780 KB |
Output is correct |
74 |
Correct |
350 ms |
2776 KB |
Output is correct |
75 |
Correct |
374 ms |
2792 KB |
Output is correct |
76 |
Correct |
412 ms |
2828 KB |
Output is correct |
77 |
Correct |
369 ms |
2808 KB |
Output is correct |
78 |
Correct |
22 ms |
2748 KB |
Output is correct |
79 |
Correct |
357 ms |
2784 KB |
Output is correct |
80 |
Correct |
352 ms |
2724 KB |
Output is correct |
81 |
Correct |
355 ms |
2836 KB |
Output is correct |
82 |
Correct |
364 ms |
2788 KB |
Output is correct |
83 |
Correct |
324 ms |
2740 KB |
Output is correct |
84 |
Correct |
319 ms |
2628 KB |
Output is correct |
85 |
Correct |
396 ms |
2644 KB |
Output is correct |
86 |
Correct |
344 ms |
2644 KB |
Output is correct |
87 |
Correct |
344 ms |
2640 KB |
Output is correct |
88 |
Correct |
330 ms |
2580 KB |
Output is correct |
89 |
Correct |
346 ms |
2532 KB |
Output is correct |
90 |
Correct |
336 ms |
2544 KB |
Output is correct |
91 |
Correct |
350 ms |
2556 KB |
Output is correct |
92 |
Correct |
332 ms |
2544 KB |
Output is correct |