Submission #953288

# Submission time Handle Problem Language Result Execution time Memory
953288 2024-03-25T19:25:56 Z sandry24 Mecho (IOI09_mecho) C++17
84 / 100
617 ms 7252 KB
#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){ //type = 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;
    }
    dist[en.f][en.s] = 1e9; //bees cant go inside home
    //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();
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 2 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 2 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 350 ms 6724 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 2 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 1 ms 4528 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4444 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 1 ms 4444 KB Output is correct
21 Correct 1 ms 4440 KB Output is correct
22 Correct 1 ms 4440 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 4696 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 4696 KB Output is correct
29 Correct 1 ms 4700 KB Output is correct
30 Correct 1 ms 4952 KB Output is correct
31 Correct 2 ms 4696 KB Output is correct
32 Correct 1 ms 4700 KB Output is correct
33 Correct 8 ms 5468 KB Output is correct
34 Correct 8 ms 5468 KB Output is correct
35 Correct 102 ms 5648 KB Output is correct
36 Correct 8 ms 5720 KB Output is correct
37 Correct 8 ms 5724 KB Output is correct
38 Correct 137 ms 5720 KB Output is correct
39 Correct 11 ms 5980 KB Output is correct
40 Correct 13 ms 5980 KB Output is correct
41 Correct 176 ms 5980 KB Output is correct
42 Correct 11 ms 5976 KB Output is correct
43 Correct 20 ms 5980 KB Output is correct
44 Correct 237 ms 6128 KB Output is correct
45 Correct 16 ms 6232 KB Output is correct
46 Correct 16 ms 6236 KB Output is correct
47 Correct 261 ms 6292 KB Output is correct
48 Correct 18 ms 6232 KB Output is correct
49 Correct 22 ms 6244 KB Output is correct
50 Correct 337 ms 6444 KB Output is correct
51 Correct 21 ms 6492 KB Output is correct
52 Correct 22 ms 6492 KB Output is correct
53 Correct 395 ms 6372 KB Output is correct
54 Correct 27 ms 6748 KB Output is correct
55 Correct 26 ms 6748 KB Output is correct
56 Correct 452 ms 6760 KB Output is correct
57 Correct 24 ms 6488 KB Output is correct
58 Correct 32 ms 6748 KB Output is correct
59 Correct 617 ms 6876 KB Output is correct
60 Correct 29 ms 6748 KB Output is correct
61 Correct 26 ms 6748 KB Output is correct
62 Correct 560 ms 6784 KB Output is correct
63 Correct 161 ms 6740 KB Output is correct
64 Correct 532 ms 6788 KB Output is correct
65 Correct 381 ms 6632 KB Output is correct
66 Correct 228 ms 6788 KB Output is correct
67 Correct 168 ms 6628 KB Output is correct
68 Correct 105 ms 6664 KB Output is correct
69 Correct 83 ms 6736 KB Output is correct
70 Correct 60 ms 6740 KB Output is correct
71 Correct 59 ms 6736 KB Output is correct
72 Incorrect 47 ms 6652 KB Output isn't correct
73 Incorrect 104 ms 7108 KB Output isn't correct
74 Correct 223 ms 6996 KB Output is correct
75 Correct 195 ms 6992 KB Output is correct
76 Correct 181 ms 6992 KB Output is correct
77 Correct 182 ms 6992 KB Output is correct
78 Correct 183 ms 7068 KB Output is correct
79 Correct 201 ms 7064 KB Output is correct
80 Correct 186 ms 7064 KB Output is correct
81 Correct 230 ms 6996 KB Output is correct
82 Correct 209 ms 7252 KB Output is correct
83 Correct 204 ms 6992 KB Output is correct
84 Correct 323 ms 6996 KB Output is correct
85 Correct 255 ms 7020 KB Output is correct
86 Correct 257 ms 7020 KB Output is correct
87 Correct 298 ms 7016 KB Output is correct
88 Correct 257 ms 6960 KB Output is correct
89 Correct 423 ms 6792 KB Output is correct
90 Correct 299 ms 6956 KB Output is correct
91 Correct 311 ms 6736 KB Output is correct
92 Correct 297 ms 6792 KB Output is correct