Submission #578704

# Submission time Handle Problem Language Result Execution time Memory
578704 2022-06-17T17:17:53 Z a_aguilo Mecho (IOI09_mecho) C++14
100 / 100
269 ms 6204 KB
#include<bits/stdc++.h>

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;

typedef vector<char> vc;
typedef vector<vc> vvc;

int INF = 1e9;
int n, s;

vvi FloodFill (vector<pair<int, int>>& V, vvc& CharMap){
    vvi ans(n, vi(n, INF));
    queue<pair<int, int>> Q;
    for(pair<int, int> start : V){
        Q.push(start);
        ans[start.first][start.second] = 0;
    }
    while(!Q.empty()){
        pair<int, int> act = Q.front(); Q.pop();
        int Xact = act.first;
        int Yact = act.second;
        if(Xact > 0 and ans[Xact-1][Yact] == INF and (CharMap[Xact-1][Yact] == 'G' or CharMap[Xact-1][Yact] == 'M')){
            ans[Xact-1][Yact] = ans[Xact][Yact]+1;
            Q.push(make_pair(Xact-1, Yact));
        }
        if (Yact > 0 and ans[Xact][Yact-1]== INF and (CharMap[Xact][Yact-1] == 'G' or CharMap[Xact][Yact-1] == 'M')){
            ans[Xact][Yact-1] = ans[Xact][Yact]+1;
            Q.push(make_pair(Xact, Yact-1));
        }
        if(Xact < (n-1) and ans[Xact+1][Yact] == INF and (CharMap[Xact+1][Yact] == 'G' or CharMap[Xact+1][Yact] == 'M')){
            ans[Xact+1][Yact] = ans[Xact][Yact]+1;
            Q.push(make_pair(Xact+1, Yact));
        }
        if(Yact < (n-1) and ans[Xact][Yact+1] == INF and (CharMap[Xact][Yact+1] == 'G' or CharMap[Xact][Yact+1] == 'M')){
            ans[Xact][Yact+1] = ans[Xact][Yact]+1;
            Q.push(make_pair(Xact, Yact+1));
        }
    }
    return ans;
}

bool bfsMecho (pair<int, int> StartPos, int initialTime, vvi& listaAdy, vvc& CharMap){
    vvi visited (n, vi(n, 0));
    visited[StartPos.first][StartPos.second] = 1;
    if(initialTime >= listaAdy[StartPos.first][StartPos.second]) return false;
    queue<pair<int, pair<int, int>>> Q;
    Q.push(make_pair(initialTime*s, StartPos));
    while(!Q.empty()){
        pair<int, pair<int, int>> act = Q.front(); Q.pop();
        int PasosAct = act.first;
        int MinuteAct = (PasosAct+1)/s;
        int x = act.second.first;
        int y = act.second.second;

        if(CharMap[x][y] == 'D') return true;

        if(x > 0 and MinuteAct < listaAdy[x-1][y] and (CharMap[x-1][y] == 'G' or CharMap[x-1][y] == 'D') and !visited[x-1][y] ){
            Q.push(make_pair(PasosAct+1, make_pair(x-1, y)));
            visited[x-1][y] = 1;
        }
        if(y > 0 and MinuteAct < listaAdy[x][y-1] and (CharMap[x][y-1] == 'G' or CharMap[x][y-1] == 'D') and !visited[x][y-1]){
            Q.push(make_pair(PasosAct+1, make_pair(x, y-1)));
            visited[x][y-1] = 1;
        }
        if(x < (n-1) and MinuteAct < listaAdy[x+1][y] and (CharMap[x+1][y] == 'G' or CharMap[x+1][y] == 'D') and !visited[x+1][y]){
            Q.push(make_pair(PasosAct+1, make_pair(x+1, y)));
            visited[x+1][y] = 1;
        }
        if(y < (n-1) and MinuteAct < listaAdy[x][y+1] and (CharMap[x][y+1] == 'G' or CharMap[x][y+1] == 'D') and !visited[x][y+1] ){
            Q.push(make_pair(PasosAct+1, make_pair(x, y+1)));
            visited[x][y+1] = 1;
        }
    }
    return false;
}

int main(){
    char c;
    cin >> n >> s;
    vector<pair<int, int>> IniciosAbejas;
    pair<int, int> InicioMecho;
    vvc mapa(n, vc(n));
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < n; ++j){
            cin >> c;
            //T = tree --> no one can pass
            //M = mecho initially
            //D = target
            //G = grass
            //H = hive
            if(c == 'M') InicioMecho = make_pair(i, j);
            else if (c == 'H') IniciosAbejas.push_back(make_pair(i, j));
            mapa[i][j] = c;
        }
    }
    vvi Matrix = FloodFill(IniciosAbejas, mapa);

    int k = -1;
    int hi = n*n;
    int lo = 0;
    while(hi >= lo){
        int mid = lo + (hi-lo)/2;
        if(bfsMecho(InicioMecho, mid, Matrix, mapa)){
            lo = mid+1;
            k = mid;
        }
        else hi = mid-1;
    }
    cout << k << endl;

    /*
    for(int i = 0; i < n; ++i){
        cout << Matrix[i][0];
        for(int j = 1; j < n; ++j){
            cout << " " << Matrix[i][j];
        }
        cout << endl;
    }*/

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 127 ms 6076 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 13 ms 1404 KB Output is correct
34 Correct 12 ms 1412 KB Output is correct
35 Correct 27 ms 1424 KB Output is correct
36 Correct 15 ms 1748 KB Output is correct
37 Correct 15 ms 1748 KB Output is correct
38 Correct 32 ms 1748 KB Output is correct
39 Correct 18 ms 2120 KB Output is correct
40 Correct 20 ms 2104 KB Output is correct
41 Correct 46 ms 2216 KB Output is correct
42 Correct 26 ms 2528 KB Output is correct
43 Correct 23 ms 2620 KB Output is correct
44 Correct 66 ms 2556 KB Output is correct
45 Correct 29 ms 2980 KB Output is correct
46 Correct 30 ms 3012 KB Output is correct
47 Correct 63 ms 3052 KB Output is correct
48 Correct 35 ms 3568 KB Output is correct
49 Correct 36 ms 3508 KB Output is correct
50 Correct 84 ms 3568 KB Output is correct
51 Correct 38 ms 4120 KB Output is correct
52 Correct 39 ms 4076 KB Output is correct
53 Correct 102 ms 4128 KB Output is correct
54 Correct 48 ms 4728 KB Output is correct
55 Correct 53 ms 4736 KB Output is correct
56 Correct 124 ms 4688 KB Output is correct
57 Correct 51 ms 5356 KB Output is correct
58 Correct 51 ms 5368 KB Output is correct
59 Correct 136 ms 5360 KB Output is correct
60 Correct 71 ms 6072 KB Output is correct
61 Correct 61 ms 6104 KB Output is correct
62 Correct 156 ms 6044 KB Output is correct
63 Correct 156 ms 6040 KB Output is correct
64 Correct 269 ms 6028 KB Output is correct
65 Correct 226 ms 6088 KB Output is correct
66 Correct 176 ms 6140 KB Output is correct
67 Correct 172 ms 6036 KB Output is correct
68 Correct 92 ms 6060 KB Output is correct
69 Correct 98 ms 6064 KB Output is correct
70 Correct 80 ms 6080 KB Output is correct
71 Correct 86 ms 6084 KB Output is correct
72 Correct 69 ms 6084 KB Output is correct
73 Correct 74 ms 6100 KB Output is correct
74 Correct 103 ms 6100 KB Output is correct
75 Correct 110 ms 6140 KB Output is correct
76 Correct 117 ms 6064 KB Output is correct
77 Correct 107 ms 6100 KB Output is correct
78 Correct 113 ms 6056 KB Output is correct
79 Correct 88 ms 6088 KB Output is correct
80 Correct 103 ms 6076 KB Output is correct
81 Correct 110 ms 6084 KB Output is correct
82 Correct 105 ms 6088 KB Output is correct
83 Correct 114 ms 6100 KB Output is correct
84 Correct 111 ms 6080 KB Output is correct
85 Correct 112 ms 6200 KB Output is correct
86 Correct 111 ms 6068 KB Output is correct
87 Correct 108 ms 6076 KB Output is correct
88 Correct 132 ms 6064 KB Output is correct
89 Correct 113 ms 6068 KB Output is correct
90 Correct 120 ms 6092 KB Output is correct
91 Correct 125 ms 6104 KB Output is correct
92 Correct 126 ms 6204 KB Output is correct