Cod sursa(job #2693772)

Utilizator DariusGhercaDarius Gherca DariusGherca Data 6 ianuarie 2021 22:40:22
Problema Barbar Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.68 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("barbar.in");
ofstream g("barbar.out");
int dl[]={1,0,-1,0};
int dc[]={0,1,0,-1};
struct pct{
    int l,c;
};
queue <pct> auxq;
queue <pct> q;
const int N=1e3+10;
int mat[N][N],n,m,mat_rez[N][N];
pct st[N*N];
pct start,stop;
void bfs()
{
    while(!auxq.empty())
    {
        int i,j;
        i=auxq.front().l;
        j=auxq.front().c;
        auxq.pop();
        for(int k=0;k<4;k++)
        {
            int ix,jx;
            ix=i+dl[k];
            jx=j+dc[k];
            if(mat[ix][jx]!=-1)
            {
                pct aux;
                aux.l=ix;
                aux.c=jx;
                q.push(aux);
                mat[ix][jx]=1;
            }
        }
    }
    while(!q.empty())
    {
        int i,j,delta;
        i=q.front().l;
        j=q.front().c;
        delta=mat[i][j]+1;
        for(int k=0;k<4;k++)
        {
            int ix,jx;
            ix=i+dl[k];
            jx=j+dc[k];
            if(mat[ix][jx]!=-1)
            {
                if(delta<mat[ix][jx] && mat[ix][jx]!=0)
                {
                    mat[ix][jx]=delta;
                    pct aux;
                    aux.l=ix;
                    aux.c=jx;
                    q.push(aux);
                }
                else
                if(mat[ix][jx]==0)
                {
                    mat[ix][jx]=delta;
                    pct aux;
                    aux.l=ix;
                    aux.c=jx;
                    q.push(aux);
                }
            }
        }
        q.pop();
    }

}
void afisare()
{
    g<<"\n\n";
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(mat_rez[i][j]==0)
            {
                g<<mat[i][j]<<" ";
            }
            else
            {
                g<<"D"<<" ";
            }
        }
        g<<"\n";
    }
    g<<"\n\n";
}
void completare_rezultat(pct k)
{
    pct aux;
    for(int i=0;i<4;i++)
    {
        int ix,jx;
        ix=k.l+dl[i];
        jx=k.c+dc[i];
        if(mat[ix][jx]!=-1)
        {
            int rez=min(mat_rez[k.l][k.c],mat[ix][jx]);
            if(mat_rez[ix][jx]==0 || mat_rez[ix][jx]<rez)
            {
                mat_rez[ix][jx]=rez;
                aux.l=ix;
                aux.c=jx;
                completare_rezultat(aux);
            }
        }
    }
}
int main()
{
    f>>n>>m;
    for(int i=0;i<=n+1;i++)
    {
        mat[i][0]=mat[i][n+1]=mat[0][i]=mat[n+1][i]=-1;
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            char c;
            f>>c;
            if(c=='.')
            {
                mat[i][j]=0;
            }
            else
            if(c=='I')
            {
                start.l=i;
                start.c=j;
                mat[i][j]=0;
            }
            else
            if(c=='D')
            {
                pct aux;
                aux.l=i;
                aux.c=j;
                mat[i][j]=-1;
                auxq.push(aux);
                mat_rez[i][j]=-1;
            }
            else
            if(c=='*')
            {
                mat[i][j]=-1;
                mat_rez[i][j]=-1;
            }
            else
            if(c=='O')
            {
                stop.l=i;
                stop.c=j;
                mat[i][j]=0;
            }
        }
    }
    bfs();
    mat_rez[start.l][start.c]=mat[start.l][start.c];
    completare_rezultat(start);
    if(mat_rez[stop.l][stop.c]==0)
    {
        g<<-1;
        return 0;
    }
    g<<mat_rez[stop.l][stop.c];
    return 0;
}