Cod sursa(job #2922616)

Utilizator PowerPlantNICOLAS ANDREI MANASIA PowerPlant Data 9 septembrie 2022 10:17:13
Problema Barbar Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.13 kb
#include <fstream>
#include <queue>

using namespace std;
ifstream fin ("barbar.in");
ofstream fout ("barbar.out");
int n,m,Max,P1,P2,Q1,Q2;
int a[1010][1010],vis[1010][1010];
queue <int> q;
void Lee_creare(){
    while(!q.empty())
    {
        int x1=q.front();
        q.pop();
        int y1=q.front();
        q.pop();
        if(vis[x1+1][y1]==0)
        {
            vis[x1+1][y1]=1;
            a[x1+1][y1]=a[x1][y1]+1;
            q.push(x1+1);
            q.push(y1);
            Max=max(Max,a[x1+1][y1]);
        }
        if(vis[x1-1][y1]==0)
        {
            vis[x1-1][y1]=1;
            a[x1-1][y1]=a[x1][y1]+1;
            q.push(x1-1);
            q.push(y1);
            Max=max(Max,a[x1-1][y1]);

        }
        if(vis[x1][y1-1]==0)
        {
            vis[x1][y1-1]=1;
            a[x1][y1-1]=a[x1][y1]+1;
            q.push(x1);
            q.push(y1-1);
            Max=max(Max,a[x1][y1-1]);
        }
        if(vis[x1][y1+1]==0)
        {
            vis[x1][y1+1]=1;
            a[x1][y1+1]=a[x1][y1]+1;
            q.push(x1);
            q.push(y1+1);
            Max=max(Max,a[x1][y1+1]);
        }
    }
}
bool Lee(int par){
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            if(a[i][j]!=-1)
                vis[i][j]=0;
        }
    q.push(P1);
    q.push(Q1);
    while(!q.empty())
    {
        int x1=q.front();
        q.pop();
        int y1=q.front();
        q.pop();
        if(vis[x1+1][y1]==0 && a[x1+1][y1]>=par)
        {
            vis[x1+1][y1]=1;
            q.push(x1+1);
            q.push(y1);
        }
        if(vis[x1-1][y1]==0 && a[x1-1][y1]>=par)
        {
            vis[x1-1][y1]=1;
            q.push(x1-1);
            q.push(y1);

        }
        if(vis[x1][y1-1]==0 && a[x1][y1-1]>=par)
        {
            vis[x1][y1-1]=1;
            q.push(x1);
            q.push(y1-1);
        }
        if(vis[x1][y1+1]==0 && a[x1][y1+1]>=par)
        {
            vis[x1][y1+1]=1;
            q.push(x1);
            q.push(y1+1);
        }
    }
    return vis[P2][Q2];

}
int main()
{
    fin>>n>>m;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            char ch;
            fin>>ch;
            if(ch=='I')
            {
                P1=i;
                Q1=j;
            }
            if(ch=='O')
            {
                P2=i;
                Q2=j;
            }
            if(ch=='*')
            {
                a[i][j]=-1;
                vis[i][j]=1;
            }
            if(ch=='D')
            {
                q.push(i);
                q.push(j);
            }
        }
    for(int i=1;i<=n;i++){
        a[i][0]=a[i][m+1]=-1;
        vis[i][0]=vis[i][m+1]=1;
    }
    for(int i=1;i<=m;i++){
        a[0][i]=a[n+1][i]=-1;
        vis[0][i]=vis[n+1][i]=1;
        }
    Lee_creare();

    int l=0,r=Max,rez=-1;
    while(l<=r)
    {
        int mid=(l+r)/2;
        if(Lee(mid)==1)
        {
            rez=mid;
            l=mid+1;
        }
        else r=mid-1;
    }
    fout<<rez;
    return 0;
}