Cod sursa(job #2481766)

Utilizator Andy_ANDYSlatinaru Andrei Alexandru Andy_ANDY Data 27 octombrie 2019 13:23:19
Problema Barbar Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.91 kb

#include <bits/stdc++.h>
#define ll long long
using namespace std;
ifstream f ("barbar.in"); ofstream g ("barbar.out");
int n,m,cost[1005][1005];
bool pos[1005][1005];
int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};
pair < int ,int > iesire,start;
bool ok ( int i,int j)
{
    return (1<=i and i<=n and 1<=j and j<=m);
}
bool lee(int val)
{
    queue < pair < int ,int > > q;
    bool viz[1005][1005]={0};
    q.push(start);
    viz[start.first][ start.second ]=1;
    while(!q.empty())
    {   int i=q.front().first;
        int j=q.front().second;
        q.pop();
        for(int k=0;k<4;k++)
        {   int ii=i+dx[k];
            int jj=j+dy[k];
            if(ok(ii,jj) and !viz[ii][jj] and !pos[ii][jj] and cost[ii][jj]>=val)
            {   viz[ii][jj]=1;
                q.push({ii,jj});
            }
        }
    }
    return viz[ iesire.first ][ iesire.second];
}
int main()
{   ios_base::sync_with_stdio(0);
    cin.tie(0);
    queue < pair < int ,int > > dragoni;
    f>>n>>m;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {   char x;
            f>>x;
            cost[i][j]=(1<<30);
            if(x=='*') pos[i][j]=1;
            if(x=='D') dragoni.push({i,j}),cost[i][j]=0;
            if(x=='O') iesire={i,j};
            if(x=='I') start={i,j};
        }
    while(!dragoni.empty())
    {   int i=dragoni.front().first;
        int j=dragoni.front().second;
        dragoni.pop();
        for(int k=0;k<4;k++)
        {   int ii=i+dx[k];
            int jj=j+dy[k];
            if(ok(ii,jj) and cost[ii][jj]>cost[i][j]+1)
            {   cost[ii][jj]=cost[i][j]+1;
                dragoni.push({ii,jj});
            }
        }
    }
    int st=1,dr=n+m,ans=-1;
    while(st<=dr)
    {   int mij=(st+dr)>>1;
        if(lee(mij))
        {   ans=max(ans,mij);
            st=mij+1;
        }
        else dr=mij-1;
    }
    g<<ans;
    return 0;
}