Cod sursa(job #3229112)

Utilizator AlinSSimilea Alin-Andrei AlinS Data 13 mai 2024 22:22:44
Problema Lowest Common Ancestor Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.22 kb
#include <bits/stdc++.h>
using namespace std;

class Task {
public:
    void solve() {
        read_input();
        get_result();
        print_output();
    }

private:
    int n; // n = latura (de la 1 la n)
    vector<pair<int, int>> paznici;

    vector<vector<char>> muzeu;
    vector<vector<int>> muzeu_d;

    void read_input() {
        ifstream fin("muzeu.in");
        fin >> n;
        muzeu = vector<vector<char>>(n + 1, vector<char>(n + 1));
        muzeu_d = vector<vector<int>>(n + 1, vector<int>(n + 1, -1));
        char c;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                fin >> c;
                muzeu[i][j] = c;

                if (c == 'P') {
                    paznici.push_back({i, j});
                }
                if (c == '#') {
                    muzeu_d[i][j] = -2;
                }
            }
        }
        fin.close();
    }

    vector<pair<int, int>> get_neighbours(pair<int, int> node) {
        vector<pair<int, int>> neighbours;
        vector<int> dx = {0, 0, -1, 1};
        vector<int> dy = {-1, 1, 0, 0};

        for (int i = 0; i < 4; i++) {
            int new_x = node.first + dx[i];
            int new_y = node.second + dy[i];

            if (new_x >= 1 && new_x <= n && new_y >= 1 && new_y <= n) {
                if (muzeu[new_x][new_y] == '.') {
                    neighbours.push_back({new_x, new_y});
                }
            }
        }

        return neighbours;
    }

    void get_result() {

        queue<pair<int, int>> q;
        vector<vector<int>> visited = vector<vector<int>>(n + 1, vector<int>(n + 1, 0));

        for (pair<int, int> p : paznici) {
            muzeu_d[p.first][p.second] = 0;
            q.push(p);
            visited[p.first][p.second] = 1;
        }

        // prima coada -> primul nivel
        // a doua coada -> urmatorul nivel
        int d = 0;
        while (!q.empty()) {
            queue<pair<int, int>> q_aux;
            d++;

            while (!q.empty()) {
                pair<int, int> node = q.front();
                q.pop();

                for (pair<int, int> neigh : get_neighbours(node)) {
                    if (visited[neigh.first][neigh.second] == 0) {
                        muzeu_d[neigh.first][neigh.second] = d;

                        q_aux.push(neigh);
                        visited[neigh.first][neigh.second] = 1;
                    }
                }
            }

            q = q_aux;
        }
    }

    void print_output() {
        ofstream fout("muzeu.out");
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                fout << muzeu_d[i][j] << " ";
            }
            fout << "\n";
        }
        fout.close();
    }
};

// [ATENTIE] NU modifica functia main!
int main() {
    // * se aloca un obiect Task pe heap
    // (se presupune ca e prea mare pentru a fi alocat pe stiva)
    // * se apeleaza metoda solve()
    // (citire, rezolvare, printare)
    // * se distruge obiectul si se elibereaza memoria
    auto* task = new (nothrow) Task(); // hint: cppreference/nothrow
    if (!task) {
        cerr << "new failed: WTF are you doing? Throw your PC!\n";
        return -1;
    }
    task->solve();
    delete task;
    return 0;
}