Submission #924092

#TimeUsernameProblemLanguageResultExecution timeMemory
924092tombMecho (IOI09_mecho)C++17
29 / 100
1082 ms65536 KiB
#include<bits/stdc++.h>
using namespace std;
#define vi vector<int>
#define ll long long
#define pii pair<int, int>
int n, s;
char grid[800][800];
queue<pair<int, int>> bees_start;
pair<int, int> mecho, home;



bool possible(int t){
    // cout << "t = " << t << endl;
    //maybe use set to avoid duplicates
    map<pii, int> bee_vis;
    map<pii, int> mecho_vis;
    queue<pii> bees = bees_start;
    queue<pii> frontier;
    frontier.push(mecho);
    //maybe store bee queues for every t if TLE...
    int bees_grid[n][n];
    memset(bees_grid, 0, sizeof(bees_grid));
    while (t--){
        // cout << "initial" << endl;
        int sz = (int) bees.size();
        for (int _ = 0; _ < sz; _++){
            auto bee = bees.front();
            bee_vis[bee] = 1;
            bees.pop();
            if (bee.first > 0 && grid[bee.first - 1][bee.second] == 'G' && !bee_vis[{bee.first - 1, bee.second}]) bees.push({bee.first - 1, bee.second}), bees_grid[bee.first - 1][bee.second] = 1;
            if (bee.first < n - 1 && grid[bee.first + 1][bee.second] == 'G' && !bee_vis[{bee.first + 1, bee.second}]) bees.push({bee.first + 1, bee.second}), bees_grid[bee.first + 1][bee.second] = 1;

            if (bee.second > 0 && grid[bee.first][bee.second - 1] == 'G' && !bee_vis[{bee.first, bee.second - 1}]) bees.push({bee.first, bee.second - 1}), bees_grid[bee.first][bee.second - 1] = 1;
            if (bee.second < n - 1 && grid[bee.first][bee.second + 1] == 'G' && !bee_vis[{bee.first, bee.second + 1}]) bees.push({bee.first, bee.second + 1}), bees_grid[bee.first][bee.second + 1] = 1;
        
        
        } 

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




    
    bool reached_home = false;
    while (frontier.size()){
        int size = (int) frontier.size();
        for (int _ = 0; _ < size; _++){

            
            // cout << frontier.size() << endl;
            // cout << "iterating" << endl;
            auto pos = frontier.front();

            if (pos == home) return true;
            frontier.pop();
            if (pos.first < 0 || pos.second < 0 || pos.first > n - 1 || pos.second > n - 1 || grid[pos.first][pos.second] == 'T' || bees_grid[pos.first][pos.second]) continue;
            // if (pos.second == 2 && pos.first == 2){
            //     cout << pos.first << " " << pos.second << endl;
            //     cout << bees_grid[pos.first][pos.second] << endl;
            // }
            mecho_vis[pos] = true;
            queue<pair<pii, int>> tmp_frontier; //let him take pair.second s steps
            tmp_frontier.push({pos, 0});
            while (tmp_frontier.size()){
                auto location = tmp_frontier.front().first;
                int steps = tmp_frontier.front().second;
                tmp_frontier.pop();
                if (location == home) return true;
                if (location.first < 0 || location.second < 0 || location.first > n - 1 || location.second > n - 1 || grid[location.first][location.second] == 'T' || bees_grid[location.first][location.second]) continue;
                // if (pos.second == 2 && pos.first == 2)
                //     cout << location.first << " " << location.second << " with " << steps << " steps" << endl;
                // mecho_vis[location] = 1;
                frontier.push(location);
                if (steps +1 <= s){
                    tmp_frontier.push({{location.first + 1, location.second}, steps + 1});
                    tmp_frontier.push({{location.first - 1, location.second}, steps + 1});
                    tmp_frontier.push({{location.first, location.second + 1}, steps + 1});
                    tmp_frontier.push({{location.first, location.second - 1}, steps + 1});

                }

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

        // cout << endl << endl;
        }
        int sz = (int) bees.size();
        for (int _ = 0; _ < sz; _++){
            auto bee = bees.front();
            bee_vis[bee] = 1;
            bees.pop();
            if (bee.first > 0 && grid[bee.first - 1][bee.second] == 'G' && !bee_vis[{bee.first - 1, bee.second}]) bees.push({bee.first - 1, bee.second}), bees_grid[bee.first - 1][bee.second] = 1;
            if (bee.first < n - 1 && grid[bee.first + 1][bee.second] == 'G' && !bee_vis[{bee.first + 1, bee.second}]) bees.push({bee.first + 1, bee.second}), bees_grid[bee.first + 1][bee.second] = 1;

            if (bee.second > 0 && grid[bee.first][bee.second - 1] == 'G' && !bee_vis[{bee.first, bee.second - 1}]) bees.push({bee.first, bee.second - 1}), bees_grid[bee.first][bee.second - 1] = 1;
            if (bee.second < n - 1 && grid[bee.first][bee.second + 1] == 'G' && !bee_vis[{bee.first, bee.second + 1}]) bees.push({bee.first, bee.second + 1}), bees_grid[bee.first][bee.second + 1] = 1;
        }

    // for (int i = 0; i < n; i++){
    //     for (int j = 0; j < n; j++){
    //         cout << bees_grid[i][j] << " ";
    //     }
    //     cout << endl;
    // }
    // cout << endl << endl;
    }
    return reached_home;
}

int main(){
    cin >> n >> s;
    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++){
            cin >> grid[i][j];
            if (grid[i][j] == 'M') mecho = {i, j}, grid[i][j] = 'G';
            if (grid[i][j] == 'D') home = {i, j};
            if (grid[i][j] == 'H') bees_start.push({i, j});
        }
    }



    int lo = 0, hi = 640000;
    while (lo < hi){
        int mid = (lo + hi + 1) / 2;
        if (possible(mid))
            lo = mid;
        else hi = mid - 1;
    }
    cout << lo << endl;
    // cout << possible(2) << endl;

    // cout << home.first << " " << home.second << endl;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...