Cod sursa(job #2531716)

Utilizator Dusceac_Bogdan24Dusceac Bogdan Dusceac_Bogdan24 Data 26 ianuarie 2020 17:27:01
Problema Barbar Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.44 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];
        }
    }
    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);
            }
        }
    }
    int distance = 0;
    for(int i = (1<<11); i >0; i /= 2)
    {
        if(verif(distance + i))
            distance += i;
    }
    if(!distance)
        distance = -1;
    fout << distance-1;
    return 0;
}