Cod sursa(job #2075732)

Utilizator stefdascalescuStefan Dascalescu stefdascalescu Data 25 noiembrie 2017 17:13:33
Problema Barbar Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.63 kb
#include<bits/stdc++.h>
using namespace std;
ifstream f("barbar.in");
ofstream g("barbar.out");
int n,m,mat[1002][1002],mat2[1002][1002];
struct punct{int l,c;};
char c[1002];
int stl,stc;
int sfl,sfc;
void init()
{
    f>>n>>m;
    for(int i=1;i<=n;++i)
    {
        f>>c+1;
        for(int j=1;j<=m;++j){
            if(c[j]=='I'){
                stl=i,stc=j;
                continue;
            }
            if(c[j]=='O'){
                sfl=i,sfc=j;
                continue;
            }
            if(c[j]=='*'){
                mat[i][j]=-1;
                continue;
            }
            if(c[j]=='D'){
                mat[i][j]=-2;
                continue;
            }
        }
    }
}
void bin_search()
{
    int b=1;
    int e=max(n,m);
    deque<punct>dr;
    deque<int>dist;
    int maxsol=-1;
    while(b<=e)
    {
        int mi=(b+e)/2;
        for(int i=1;i<=n;++i)
            for(int j=1;j<=m;++j){
                punct z;
                z.l=i;
                z.c=j;
                if(mat[i][j]==-2)
                    dr.push_back(z),dist.push_back(0);
            }
        memcpy(mat2,mat,sizeof(mat));
        while(!dr.empty())
        {
            if(dist[0]+1<=mi)
            {
                int L=dr[0].l;
                int C=dr[0].c;
                if(L>1 && mat2[L-1][C]==0){
                    punct z;
                    z.l=L-1;
                    z.c=C;
                    mat2[L-1][C]=-2,dr.push_back(z),dist.push_back(dist[0]+1);
                }
                if(C>1 && mat2[L][C-1]==0){
                    punct z;
                    z.l=L;
                    z.c=C-1;
                    mat2[L][C-1]=-2,dr.push_back(z),dist.push_back(dist[0]+1);
                }
                if(L<n && mat2[L+1][C]==0){
                    punct z;
                    z.l=L+1;
                    z.c=C;
                    mat2[L+1][C]=-2,dr.push_back(z),dist.push_back(dist[0]+1);
                }
                if(C<m && mat2[L][C+1]==0){
                    punct z;
                    z.l=L;
                    z.c=C+1;
                    mat2[L][C+1]=-2,dr.push_back(z),dist.push_back(dist[0]+1);
                }
            }
            dr.pop_front();
            dist.pop_front();
        }
        deque<punct>v;
        if(mat2[stl][stc]==0)
        {
            punct z;
            z.l=stl;
            z.c=stc;
            v.push_back(z);
            mat2[stl][stc]=1;
        }
        while(!v.empty())
        {
            int L=v[0].l;
            int C=v[0].c;
            if(L>1 && mat2[L-1][C]==0){
                punct z;
                z.l=L-1;
                z.c=C;
                mat2[L-1][C]=1,v.push_back(z);
            }
            if(C>1 && mat2[L][C-1]==0){
                punct z;
                z.l=L;
                z.c=C-1;
                mat2[L][C-1]=1,v.push_back(z);
            }
            if(L<n && mat2[L+1][C]==0){
                punct z;
                z.l=L+1;
                z.c=C;
                mat2[L+1][C]=1,v.push_back(z);
            }
            if(C<m && mat2[L][C+1]==0){
                punct z;
                z.l=L;
                z.c=C+1;
                mat2[L][C+1]=1,v.push_back(z);
            }
            if(mat2[sfl][sfc]==1)
                break;
            v.pop_front();
        }
        if(mat2[sfl][sfc]==1)
            maxsol=mi,b=mi+1;
        else
            e=mi-1;
    }
    if(maxsol==-1)
        g<<maxsol;
    else
        g<<maxsol+1;
}
int main()
{
    init();
    bin_search();
    return 0;
}