Cod sursa(job #2665302)

Utilizator Silviu.Stancioiu@gmail.comSilviu Stancioiu [email protected] Data 30 octombrie 2020 15:33:32
Problema Barbar Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.39 kb
#include <fstream>
#include <iostream>
#include <queue>

constexpr auto max_size = 1005;

using namespace std;

ifstream fin("barbar.in");
ofstream fout("barbar.out");
int r, c;
char current_line[max_size] = { NULL };
int dragons_distances[max_size][max_size];
int viz[max_size][max_size];
pair<int, int> beg_pos;
pair<int, int> end_pos;
queue<pair<int, int>> bfs_queue;
int dx[] = { -1, 0, 1, 0 };
int dy[] = { 0, -1, 0, 1 };

bool valid_pos(const pair<int, int> pos)
{
    if (pos.first >= 0 && pos.first < r &&
        pos.second >= 0 && pos.second < c)
        if (dragons_distances[pos.first][pos.second] != -2)
            return true;
    
    return false;
}

bool verify(const int threshold)
{
    if (dragons_distances[beg_pos.first][beg_pos.second] < threshold)
        return false;

    viz[beg_pos.first][beg_pos.second] = threshold;
    bfs_queue.push(beg_pos);

    while (!bfs_queue.empty())
    {
        const auto crt_node = bfs_queue.front();
        bfs_queue.pop();

        for (auto i = 0; i < 4; i++)
        {
            auto new_pos = make_pair(crt_node.first + dx[i], crt_node.second + dy[i]);
            if (valid_pos(new_pos))
            {
                if (viz[new_pos.first][new_pos.second] != threshold && dragons_distances[new_pos.first][new_pos.second] >= threshold)
                {
                    viz[new_pos.first][new_pos.second] = threshold;
                    bfs_queue.push(new_pos);
                }
            }
        }
    }

    return viz[end_pos.first][end_pos.second] == threshold;
}

int main()
{
    fin >> r >> c;

    for (auto i = 0; i < r; i++)
    {
        fin >> current_line;
        for (auto j = 0; j < c; j++)
        {
            const auto chr = current_line[j];
            switch (chr)
            {
            case '.':
                dragons_distances[i][j] = -1;
                break;
            case '*':
                dragons_distances[i][j] = -2;
                break;
            case 'D':
                dragons_distances[i][j] = 0;
                bfs_queue.push(make_pair(i, j));
                break;
            case 'I':
                dragons_distances[i][j] = -1;
                beg_pos = make_pair(i, j);
                break;
            case 'O':
                dragons_distances[i][j] = -1;
                end_pos = make_pair(i, j);
                break;
            default: ;
            }
        }
    }

    auto max_dist = 0;

    /*

    while (!bfs_queue.empty())
    {
        auto& crt_node = bfs_queue.front();
        bfs_queue.pop();

        const auto dist = dragons_distances[crt_node.first][crt_node.second];
        max_dist = max(dist, max_dist);

        for (auto i = 0; i < 4; i++)
        {
            auto new_pos = make_pair(crt_node.first + dx[i], crt_node.second + dy[i]);
            if (valid_pos(new_pos))
            {
                if (dragons_distances[new_pos.first][new_pos.second] == -1)
                {
                    dragons_distances[new_pos.first][new_pos.second] = dist + 1;
                    bfs_queue.push(new_pos);
                }
            }
        }
    }

    auto index = 0;
    for (auto i = 1 << 25; i >= 1; i >>= 1)
        if (index + i <= max_dist)
            if (verify(index + i))
                index += i;

    fout << index;*/

    return 0;
}