Cod sursa(job #1498313)

Utilizator bogdanpaunFMI Paun Bogdan Gabriel bogdanpaun Data 8 octombrie 2015 13:28:13
Problema Barbar Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.55 kb
#include <bits/stdc++.h>

using namespace std;
#define N 1002
int l , c , k , m , p , q , t    ;
char a[N][N];
int xi,yi,xo,yo,x,y,z;
unsigned b[N][N],cc[N][N];
int dx[] = {-1,0,1, 0};
int dy[] = {0 ,1,0,-1};

queue< pair<int,int> >qq;

#define debu

int main()
{
    freopen("barbar.in" , "r" , stdin);
    freopen("barbar.out", "w" , stdout);
    int i,j;

    scanf("%d %d\n", &l, &c);
    for(i = 1; i <= l; ++i)
        scanf("%s", a[i]+1 );

    memset( b , 1 , sizeof(b) );

    for(i=1;i<=l;++i)
        for(j=1;j<=c;++j)
            if( a[i][j] == 'I' ){
                xi=i;
                yi=j;
                a[i][j] = '.';
            }else if( a[i][j] == 'O' ){
                xo = i;
                yo = j;
                a[i][j] = '.';
            }else if( a[i][j] == 'D' ){
                qq.push( {i,j} );
                b[i][j] = 0;
            }

    for(i=0;i<=l+1;++i)
        a[i][0] = a[i][c+1] = '*';
    for(i=0;i<=c+1;++i)
        a[0][i] = a[l+1][i] = '*';


    for( ; !qq.empty() ; qq.pop() ){
        x = qq.front().first;
        y = qq.front().second;
        for(k=0;k<4;++k){
            i = x + dx[k];
            j = y + dy[k];
            if( a[i][j] != '*' && b[i][j] > b[x][y] + 1 ){
                b[i][j] = b[x][y] + 1;
                qq.push( {i,j} );
            }
        }
    }

    #ifdef debug
    for(i=1;i<=l;++i)
    {   for(j=1;j<=c;++j)
            printf("%d ",b[i][j]==16843009?0:b[i][j]);
        printf("\n");
    }
    #endif

    z = min(l,c);
    p = 1<<10;
    q = 0;
    for( ; p ; p >>= 1 ){
        if( q + p <= z ){
            if( b[xi][yi] < q + p ) continue;
            memset(cc , 0 , sizeof(cc) );
            qq.push( {xi , yi} );
            t = q + p;
            for( ; !qq.empty() ; ){
                x = qq.front().first;
                y = qq.front().second;
                for(k=0;k<4;++k){
                    i = x + dx[k];
                    j = y + dy[k];
                    if( a[i][j] == '.' && !cc[i][j] && b[i][j] >= t ){
                        cc[i][j] = 1;
                        qq.push( {i,j} );
                    }
                    if( i == xo && j == yo ){
                        while( !qq.empty() ) qq.pop();
                        break;
                    }
                }
                if( qq.empty() ) break;
                qq.pop();
            }
            if( cc[xo][yo] ){
                q += p;

            }
        }
    }

    printf("%d\n",q);
    return 0;
}