Cod sursa(job #2532918)

Utilizator Dusceac_Bogdan24Dusceac Bogdan Dusceac_Bogdan24 Data 28 ianuarie 2020 16:35:44
Problema Barbar Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.56 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("barbar.in");
ofstream fout("barbar.out");
struct strc{
    int l,c;
}pos,poss,ies,poz;
int posi[4]={0,1,0,-1};
int posj[4]={1,0,-1,0};
char c;
short m,n,st=1,dr,mid;
queue<strc>q;
int mat[1005][1005],matt[1005][1005];


void afis(){
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            fout<<mat[i][j]<<' ';
        }
        fout<<'\n';
    }
}
bool verif(int x){
    for(int i=0;i<=n+1;i++){
        for(int j=0;j<=m+1;j++){
            matt[i][j]=mat[i][j]-1;
        }
    }
    q.push(poss);
    while(!q.empty()){
        pos=q.front();
        q.pop();
        for(int k=0;k<4;k++){
            poz.l=pos.l+posi[k];
            poz.c=pos.c+posj[k];
            if(matt[poz.l][poz.c]>=x){
                matt[poz.l][poz.c]=0;
                q.push(poz);
            }
            if(poz.l==ies.l && poz.c==ies.c){
                return 1;
            }

        }
    }
    return 0;
}
int main()
{

    fin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            fin>>c;
            switch (c){
            case '*':
                mat[i][j]=-1;
                break;
            case 'I':
                poss.l=i;
                poss.c=j;
                break;
            case 'D':
                pos.l=i;
                pos.c=j;
                mat[i][j]=1;
                q.push(pos);
                break;
            case 'O':
                ies.l=i;
                ies.c=j;
                break;
            }
        }
    }
    for(int i=0;i<=n+1;i++){
        mat[i][0]=-1;
        mat[i][m+1]=-1;
    }
    for(int j=1;j<=m;j++){
        mat[0][j]=-1;
        mat[n+1][j]=-1;
    }
    while(!q.empty()){
        pos=q.front();
        q.pop();
        for(int k=0;k<4;k++){
            poz.l=pos.l+posi[k];
            poz.c=pos.c+posj[k];
            if(mat[poz.l][poz.c]>mat[pos.l][pos.c]+1){
                mat[poz.l][poz.c]=mat[pos.l][pos.c]+1;
                q.push(poz);
            }
            if(mat[poz.l][poz.c]==0){
                mat[poz.l][poz.c]=mat[pos.l][pos.c]+1;
                q.push(poz);
            }
        }
    }
    if(verif(1)==0){
        fout<<-1;
        return 0;
    }
    short maxx=1;
    dr=mat[poss.l][poss.c]-1;
    st=1;
    while(st!=dr){
        mid=(st+dr)/2;
        if(verif(mid)){
            st=mid+1;
            maxx=max(maxx,mid);
        }
        else
            dr=mid-1;
    }
    fout<<maxx;

    return 0;
}