Submission #978132

#TimeUsernameProblemLanguageResultExecution timeMemory
978132BodishaMecho (IOI09_mecho)C++17
40 / 100
187 ms7400 KiB
#include <bits/stdc++.h>
#define MAX_N 801
#define UNVISITED 0
#define BEAR 1
#define BEE 2

using namespace std;

int n, s;
char grid[MAX_N][MAX_N];
int visited[MAX_N][MAX_N];
int beed[MAX_N][MAX_N];

bool check(int t) {
    pair<int, int> mecho_pos;
    pair<int, int> home_pos;
    vector<pair<int, int>> hive_edges;
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
            visited[i][j] = UNVISITED;
            beed[i][j] = -69;
            if(grid[i][j] == 'H') {
                hive_edges.push_back({i, j});
            }
            if(grid[i][j] == 'M') {
                mecho_pos = {i, j};
            }
            if(grid[i][j] == 'D') {
                home_pos = {i, j};
            }
        }
    }
    queue<pair<int, int>> beeq;
    vector<pair<int, int>> bees_edge;
    for(auto iter : hive_edges) {
        visited[iter.first][iter.second] = BEE;
        beed[iter.first][iter.second] = 0;
        beeq.push(iter);
    }
    while(!beeq.empty()) {
        pair<int, int> curr = beeq.front();
        beeq.pop();
        if(beed[curr.first][curr.second] == t) {
            bees_edge.push_back(curr);
            continue;
        }
        if(visited[curr.first + 1][curr.second] == UNVISITED && (grid[curr.first + 1][curr.second] == 'G' || grid[curr.first + 1][curr.second] == 'M')) {
            visited[curr.first + 1][curr.second] = BEE;
            beed[curr.first + 1][curr.second] = beed[curr.first][curr.second] + 1;
            beeq.push({curr.first + 1, curr.second});
        }
        if(visited[curr.first - 1][curr.second] == UNVISITED && (grid[curr.first - 1][curr.second] == 'G' || grid[curr.first - 1][curr.second] == 'M')) {
            visited[curr.first - 1][curr.second] = BEE;
            beed[curr.first - 1][curr.second] = beed[curr.first][curr.second] + 1;
            beeq.push({curr.first - 1, curr.second});
        }
        if(visited[curr.first][curr.second + 1] == UNVISITED && (grid[curr.first][curr.second + 1] == 'G' || grid[curr.first][curr.second + 1] == 'M')) {
            visited[curr.first][curr.second + 1] = BEE;
            beed[curr.first][curr.second + 1] = beed[curr.first][curr.second] + 1;
            beeq.push({curr.first, curr.second + 1});
        }
        if(visited[curr.first][curr.second - 1] == UNVISITED && (grid[curr.first][curr.second - 1] == 'G' || grid[curr.first][curr.second - 1] == 'M')) {
            visited[curr.first][curr.second - 1] = BEE;
            beed[curr.first][curr.second - 1] = beed[curr.first][curr.second] + 1;
            beeq.push({curr.first, curr.second - 1});
        }
    }
    if(visited[mecho_pos.first][mecho_pos.second] == BEE) {
        return false;
    }
    set<pair<int, int>> mecho_edge;
    mecho_edge.insert(mecho_pos);
    bool found = false;
    while(!found) {
        if(mecho_edge.size() == 0) {
            return false;
        }
        queue<pair<pair<int, int>, int>> q; // mecho queue
        for(auto iter : mecho_edge) {
            visited[iter.first][iter.second] = BEAR;
            q.push({iter, 0});
        }
        mecho_edge.clear();
        while(!q.empty()) {
            pair<pair<int, int>, int> curr = q.front();
            q.pop();
            if(curr.first.first + 1 < n && visited[curr.first.first + 1][curr.first.second] == UNVISITED && (grid[curr.first.first + 1][curr.first.second] == 'G' || grid[curr.first.first + 1][curr.first.second] == 'D')){
                if(curr.second == s - 1) {
                    visited[curr.first.first + 1][curr.first.second] = BEAR;
                    mecho_edge.insert({curr.first.first + 1, curr.first.second});
                } else {
                    visited[curr.first.first + 1][curr.first.second] = BEAR;
                    q.push({{curr.first.first + 1, curr.first.second}, curr.second + 1});
                }
            }
            if(curr.first.first - 1 >= 0 && visited[curr.first.first - 1][curr.first.second] == UNVISITED && (grid[curr.first.first - 1][curr.first.second] == 'G' || grid[curr.first.first - 1][curr.first.second] == 'D')){
                if(curr.second == s - 1) {
                    visited[curr.first.first - 1][curr.first.second] = BEAR;
                    mecho_edge.insert({curr.first.first - 1, curr.first.second});
                } else {
                    visited[curr.first.first - 1][curr.first.second] = BEAR;
                    q.push({{curr.first.first - 1, curr.first.second}, curr.second + 1});
                }
            }
            if(curr.first.second + 1 < n && visited[curr.first.first][curr.first.second + 1] == UNVISITED && (grid[curr.first.first][curr.first.second + 1] == 'G' || grid[curr.first.first][curr.first.second + 1] == 'D')){
                if(curr.second == s - 1) {
                    visited[curr.first.first][curr.first.second + 1] = BEAR;
                    mecho_edge.insert({curr.first.first, curr.first.second + 1});
                } else {
                    visited[curr.first.first][curr.first.second + 1] = BEAR;
                    q.push({{curr.first.first, curr.first.second + 1}, curr.second + 1});
                }
            }
            if(curr.first.second - 1 >= 0 && visited[curr.first.first][curr.first.second - 1] == UNVISITED && (grid[curr.first.first][curr.first.second - 1] == 'G' || grid[curr.first.first][curr.first.second - 1] == 'D')){
                if(curr.second == s - 1) {
                    visited[curr.first.first][curr.first.second - 1] = BEAR;
                    mecho_edge.insert({curr.first.first, curr.first.second - 1});
                } else {
                    visited[curr.first.first][curr.first.second - 1] = BEAR;
                    q.push({{curr.first.first, curr.first.second - 1}, curr.second + 1});
                }
            }
        }
        if(visited[home_pos.first][home_pos.second] == BEAR) {
            found = true;
            break;
        }
        vector<pair<int, int>> tmpedge;
        for(auto iter : bees_edge) {
            if(iter.first + 1 < n && visited[iter.first + 1][iter.second] == UNVISITED && grid[iter.first + 1][iter.second] == 'G') {
                visited[iter.first + 1][iter.second] = true;
                tmpedge.push_back({iter.first + 1, iter.second});
            }
            if(iter.first - 1 >= 0 && visited[iter.first - 1][iter.second] == UNVISITED && grid[iter.first - 1][iter.second] == 'G') {
                visited[iter.first - 1][iter.second] = true;
                tmpedge.push_back({iter.first - 1, iter.second});
            }
            if(iter.second + 1 < n && visited[iter.first][iter.second + 1] == UNVISITED && grid[iter.first][iter.second + 1] == 'G') {
                visited[iter.first][iter.second + 1] = true;
                tmpedge.push_back({iter.first, iter.second + 1});
            }
            if(iter.second - 1 >= 0 && visited[iter.first][iter.second - 1] == UNVISITED && grid[iter.first][iter.second - 1] == 'G') {
                visited[iter.first][iter.second - 1] = true;
                tmpedge.push_back({iter.first, iter.second - 1});
            }
        }
        bees_edge.clear();
        bees_edge = tmpedge;
    }
    if(found) {
        return true;
    } else {
        return false;
    }
}

int main() {
    cin >> n >> s;
    for(int i = 0; i < n; i++) {
        string tmp;
        cin >> tmp;
        for(int j = 0; j < n; j++) {
            grid[i][j] = tmp[j];
        }
    }
    int l = 0, r = 2 * n;
    int ans = -1;
    // true true true ... (true) false false
    while(l <= r) {
        int mid = l + (r - l) / 2;
        if(check(mid)) {
            ans = mid;
            l = mid + 1;
        } else {
            r = mid - 1;
        }
    }
    cout << ans;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...