Cod sursa(job #3000968)

Utilizator adelina_15InfoAdelina Radoi adelina_15Info Data 13 martie 2023 09:25:52
Problema Barbar Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.92 kb
#include <iostream>
#include <fstream>
#include <queue>

using namespace std;

const int Nmax = 1005;

ifstream fin("barbar.in");
ofstream fout("barbar.out");

int n, m;
char a[Nmax][Nmax];
int drum[Nmax][Nmax];
int drum2[Nmax][Nmax];
int ii, ij;
int oi, oj;

struct cor
{
    int x, y;
};

queue<cor> cord;

bool IsOk(int x, int y)
{
    return (x > 0 && x <= n) && (y > 0 && y <= m) && (a[x][y] != '*') && (drum[x][y] == -1);
}

bool Departe(int x, int y)
{
    return (x > 0 && x <= n) && (y > 0 && y <= m) && (a[x][y] != '*') && (drum2[x][y] == -1);
}

bool DrumBun(int nr)
{
    queue<cor> aux;
    int dx[] = {-1, 0, 1, 0};
    int dy[] = {0, 1, 0, -1};
    aux.push({ii, ij});
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            drum2[i][j] = -1;

    drum2[ii][ij] = 0;
    //bool ok = false;
    while(!aux.empty())
    {
        cor temp = aux.front();
        aux.pop();
        for(int i = 0; i < 4; i++)
        {
            int xnou = temp.x+dx[i];
            int ynou = temp.y+dy[i];
            if(Departe(xnou, ynou) && drum[xnou][ynou] >= nr)
            {
                aux.push({xnou,ynou});
                drum2[xnou][ynou] = 0;
                //cout << xnou << " " << ynou << endl;
            }
            if(xnou == oi && ynou == oj)
                return true;
        }
    }
    return false;
}

int main()
{
    fin >> n >> m;
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= m; j++)
        {
            char c;
            fin >> c;
            if(c == '*' || c == 'O' || c == 'I' || c == '.' || c == 'D')
                a[i][j] = c;
            drum[i][j] = -1;
            drum2[i][j] = -1;
            if(a[i][j] == 'D')
            {
                cord.push({i, j});
                drum[i][j] = 0;
            }
            if(a[i][j] == 'I')
            {
                ii = i;
                ij = j;
                drum2[i][j] = 0;
            }
            if(a[i][j] == 'O')
            {
                oi = i;
                oj = j;
            }
        }
    }
    //cout << a[9][4];
    int dx[] = {-1, 0, 1, 0};
    int dy[] = {0, 1, 0, -1};
    while(!cord.empty())
    {
        cor temp = cord.front();
        cord.pop();
        for(int i = 0; i < 4; i++)
        {
            int xnou = temp.x+dx[i];
            int ynou = temp.y+dy[i];
            if(IsOk(xnou, ynou))
            {
                drum[xnou][ynou] = drum[temp.x][temp.y]+1;
                cord.push({xnou,ynou});
            }
        }
    }


    //cout << DrumBun(2);
    int st = 0, dr = n+m;
    int rasp = -1;
    while(st <= dr)
    {
        int mij = (st+dr)/2;
        bool rez = DrumBun(mij);
        if(rez)
        {
            rasp = mij;
            st = mij+1;
        }
        else
            dr = mij-1;
    }

    fout << rasp;

    return 0;
}