Cod sursa(job #2674539)

Utilizator toma_ariciuAriciu Toma toma_ariciu Data 19 noiembrie 2020 16:48:03
Problema Barbar Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.41 kb
#include <fstream>
#include <iostream>
#include <queue>
#include <cctype>
#include <string>

using namespace std;

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

int barbar[255][255];
int dir[4][2]={{-1,0},{0,-1},{0,1},{1,0}};
int n, m, maxi;
int portari[62505][2];
int mat[255][255];

struct Coordonate
{
    int lin, col;
};

queue <Coordonate> q;
char a[10005];

bool InBounds(Coordonate coord, int mat[255][255])
{
    return 0 <= coord.lin && coord.lin <n && 0 <= coord.col && coord.col <m;
}

bool IsFree(Coordonate coord, int mat[255][255])
{
    return mat[coord.lin][coord.col]==0;
}

bool IsShorter(Coordonate coord1, Coordonate coord2)
{
    return barbar[coord2.lin][coord2.col] + 1 < barbar[coord1.lin][coord1.col];
}

void lee()
{
    while (!q.empty())
    {
        Coordonate next_coord, current_coord=q.front();
        q.pop();
        for(int i=0; i<4; i++)
        {
            next_coord.lin=current_coord.lin+dir[i][0];
            next_coord.col=current_coord.col+dir[i][1];
            if(InBounds(next_coord, barbar) && (IsFree(next_coord, barbar) || IsShorter(next_coord, current_coord)))
            {
                barbar[next_coord.lin][next_coord.col]=barbar[current_coord.lin][current_coord.col] + 1;
                q.push(next_coord);
                if(barbar[next_coord.lin][next_coord.col]>maxi)
                    maxi=barbar[next_coord.lin][next_coord.col];
            }
        }
    }
}
void discount_lee(int x1, int y1)
{
    Coordonate coord;
    coord.lin=x1;
    coord.col=y1;
    q.push(coord);
    while (!q.empty())
    {
        Coordonate next_coord, current_coord=q.front();
        q.pop();
        for(int i=0; i<4; i++)
        {
            next_coord.lin=current_coord.lin+dir[i][0];
            next_coord.col=current_coord.col+dir[i][1];
            if(InBounds(next_coord, mat) && IsFree(next_coord, mat))
            {
                mat[next_coord.lin][next_coord.col]=1;
                q.push(next_coord);
            }
        }
    }

}

int main()
{
    int i, j, nr_portari=0;
    int x1,y1,x2,y2;
    int mini;
    char a[255];
    fin>>n>>m>>ws;
    for(i=0; i<n; i++)
    {
        fin.getline(a, 255);
        for(j=0; j<m; j++)
        {
            if(a[j]=='.')
                barbar[i][j]=0;
            if(a[j]=='*')
                barbar[i][j]=-1;
            if(a[j]=='D')
            {
                barbar[i][j]=1;
                Coordonate coord;
                coord.lin=i;
                coord.col=j;
                q.push(coord);
                nr_portari++;
            }
            if(a[j]=='I')
            {
                barbar[i][j]=0;
                x1=i;
                y1=j;
            }
            if(a[j]=='O')
            {
                barbar[i][j]=0;
                x2=i;
                y2=j;
            }
        }
    }
    lee();
    int st=1, dr=maxi, med;
    while(st<=dr)
    {
        med=(st+dr)/2;
        for(i=0; i<n; i++)
            for(j=0; j<m; j++)
            {
                if(barbar[i][j] < med)
                    mat[i][j]=-1;
                else
                    mat[i][j]=0;
            }
        discount_lee(x1, y1);
        if(mat[x2][y2]==1)
        {
            mini=med;
            st=med+1;
        }
        else dr=med-1;
    }
    fout<<mini-1;
    return 0;
}