Click Here to go back to the homepage.

Conquest Campaign Solution:


#include <bits/stdc++.h>

using namespace std;

void invade(set<pair<int, int>> *conquered, queue<pair<int, int>> *queue, int row, int col, int rows, int cols){
        (*conquered).insert(make_pair(row, col));
        if(row > 1 ){
            (*queue).push(make_pair(row-1, col));
        }
        if(row < rows){
            (*queue).push(make_pair(row+1, col));
        }
        if(col > 1){
            (*queue).push(make_pair(row, col-1));
        }
        if(col < cols){
            (*queue).push(make_pair(row, col+1));
        }
}

int main(){
    // #ifndef TESTING
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    // #endif

    int rows, cols, initial;
    cin >> rows >> cols >> initial;
    int conquered_num = rows * cols;
    queue<pair<int, int>> queue;
    set<pair<int, int>> conquered;
    for(int i = 0; i < initial; i++){
        int row, col;
        cin >> row >> col;
        invade(&conquered, &queue, row, col, rows, cols);
    }
    int day;
    for(day = 1; conquered.size() != conquered_num; day++){
        int queue_size = queue.size();
        for(int i = 0; i < queue_size; i++){
            pair<int, int> curr_pair = queue.front();
            queue.pop();
            if(conquered.find(curr_pair) != conquered.end()){
                continue;
            }
            invade(&conquered, &queue, curr_pair.first, curr_pair.second, rows, cols);
        }
    }
    cout << day;
    
    return 0;
}