Cod sursa(job #2809318)

Utilizator ilincap2008Ilinca Popescu ilincap2008 Data 26 noiembrie 2021 17:39:54
Problema Barbar Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.03 kb
#include <fstream>
#include <queue>

using namespace std;
ifstream cin("barbar.in");
ofstream cout("barbar.out");
const int Nmax = 1005;
int n, m, dist[Nmax][Nmax];
bool viz[Nmax][Nmax];
char a[Nmax][Nmax];
struct Celula
{
    int lin, col;
    const bool operator == (const Celula x)
    {
        return lin == x.lin && col == x.col;
    }
};
queue<Celula> Q;
Celula start, finish;
bool isValid(Celula cell)
{
    return (cell.lin >= 1 && cell.lin <= n && cell.col >= 1 && cell.col <= m &&
            a[cell.lin][cell.col] != '*' && dist[cell.lin][cell.col] == -1);
}

bool isValid1(Celula cell, int D)
{
    return (cell.lin >= 1 && cell.lin <= n && cell.col >= 1 && cell.col <= m &&
            a[cell.lin][cell.col] != '*' && !viz[cell.lin][cell.col] && dist[cell.lin][cell.col] >= D);
}
void Lee_Extins()
{
    const int dx[] = {-1, 0, 1,  0};
    const int dy[] = { 0, 1, 0, -1};
    while(!Q.empty())
    {
        Celula cell = Q.front();
        Q.pop();
        for(int dir = 0; dir < 4; dir++)
        {
            Celula neighbor = {cell.lin + dx[dir], cell.col + dy[dir]};
            if(isValid(neighbor))
            {
                dist[neighbor.lin][neighbor.col] = dist[cell.lin][cell.col] + 1;
                Q.push(neighbor);
            }
        }
    }
}
bool Ok(int D)
{
    const int dx[] = {-1, 0, 1,  0};
    const int dy[] = { 0, 1, 0, -1};
    if(dist[start.lin][start.col] < D)
    {
        return false;
    }
    while(!Q.empty())
    {
        Q.pop();
    }
    Q.push(start);
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= m; j++)
        {
            viz[i][j] = false;
        }
    }
    viz[start.lin][start.col] = true;
    while(!Q.empty())
    {
        Celula cell = Q.front();
        Q.pop();
        if(cell == finish)
        {
            return true;
        }
        for(int dir = 0; dir < 4; dir++)
        {
            Celula neighbor = {cell.lin + dx[dir], cell.col + dy[dir]};
            if(isValid1(neighbor, D))
            {
                viz[neighbor.lin][neighbor.col] = true;
                Q.push(neighbor);
            }
        }
    }
    return false;
}
int main()
{
    cin >> n >> m;
    for(int i = 1; i <= n; i++)
    {
        cin >> (a[i] + 1);
        for(int j = 1; j <= m; j++)
        {
            dist[i][j] = -1;
            if(a[i][j] == 'I')
            {
                start = {i, j};
            }
            else if(a[i][j] == 'O')
            {
                finish = {i, j};
            }
            else if(a[i][j] == 'D')
            {
                dist[i][j] = 0;
                Q.push({i, j});
            }
        }
    }
    Lee_Extins();
    int st = 1, dr = n * m, sol = -1;
    while(st <= dr)
    {
        int mij = (st + dr) / 2;
        if(Ok(mij))
        {
            sol = mij;
            st = mij + 1;
        }
        else
        {
            dr = mij - 1;
        }
    }
    cout << sol << "\n";
    return 0;
}