Cod sursa(job #290163)

Utilizator pedobearBacauanu Vlad pedobear Data 27 martie 2009 16:26:55
Problema Barbar Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.22 kb
#include <stdio.h>

const int INF=1500000;

int dist[1001][1001],dx[4]={0,0,-1,1},dy[4]={-1,1,0,0};
short int cx[1000001],cy[1000001];
int R,C,i,j,xst,yst,xend,yend,b,a,vx,vy,cost,st,dr,sol,mid;
char drum[1001][1001];

void citireapizdii ()
{
    char patr;
    scanf ("%d %d\n",&R,&C);
    for (i=1;i<=R;i++){
        for (j=1;j<=C;j++){
            scanf ("%c",&patr);
            if (patr=='.') dist[i][j]=INF;
            else if (patr=='D'){
                 cx[++b]=i;
                 cy[b]=j;
                 dist[i][j]=0;
                 }
            else if (patr=='*') dist[i][j]=-1;
            else if (patr=='I'){
                 xst=i;
                 yst=j;
                 dist[i][j]=INF;
                 }
            else {
                 xend=i;
                 yend=j;
                 dist[i][j]=INF;
                 }
            }
        scanf ("\n");
        }
}

void distanta ()
{
    int o=1, q=b;
    while (b-a>=0){
          cost++;
          for (a=o;a<=b;a++)
              for (i=0;i<4;i++){
                  vx=cx[a]+dx[i];
                  vy=cy[a]+dy[i];
                  if (dist[vx][vy]==INF && vx>0 && vx<=C && vy>0 && vy<=R){
                                                                           dist[vx][vy]=cost;
                                                                           cx[++q]=vx;
                                                                           cy[q]=vy;
                                                                           }
                  }
          o=b+1;
          b=q;
          }
}

void curatare ()
{
     for (i=1;i<=R;i++)
         for (j=1;j<=C;j++)
             drum[i][j]=0;
     drum[xst][yst]=1;
}

void cbin ()
{
    st=0; dr=cost-1; cx[1]=xst; cy[1]=yst;
    while (st<=dr){
          curatare ();
          mid=(st+dr)/2; b=1;
          for (a=1;a<=b;a++)
              for (i=0;i<4;i++){
                  vx=cx[a]+dx[i];
                  vy=cy[a]+dy[i];
                  if (drum[vx][vy]==0 && dist[vx][vy]>=mid && vx>0 && vx<=C && vy>0 && vy<=R){
                                                                                              drum[vx][vy]=1;
                                                                                              cx[++b]=vx;
                                                                                              cy[b]=vy;
                                                                                              }
                  }
          /*if (drum[xend][yend]==1 && dist[xst][yst]>=mid){
                                   sol=mid;
                                   st=mid+1;
                                   }
          else dr=mid-1;*/
          if (drum[xend][yend]==1){
                                   st=mid+1;
                                   }
          else {
               dr=mid-1;
               sol=mid;
               }
          }
}

int main ()
{
    freopen ("barbar.in","r",stdin);
    freopen ("barbar.out","w",stdout);
    
    citireapizdii();
    distanta ();
    cbin ();
    
    if (sol==st) printf ("%d",sol-1);
    else printf ("-1");
    
    return 0;
}