Cod sursa(job #204733)

Utilizator mihaipoascaPoasca Mihai mihaipoasca Data 26 august 2008 18:30:50
Problema Barbar Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.39 kb
#include<stdio.h>
#include<fstream>
using namespace std;



 int N,M,xi,yi,xf,yf;
 int dragoni[1002][1002],barbar[1002][1002];
 char a[1002][1002];

 int dx[]={1,0,-1,0},
     dy[]={0,1,0,-1};

 struct co{int x,y;};
 typedef co coada[1001*1000];

 ifstream fin("barbar.in");
 ofstream fout("barbar.out");

 void init(){
     for(int i=1;i<=N;i++)
         for(int j=1;j<=M;j++)
             barbar[i][j]=0;
 }

 int main(){


     fin>>N>>M;
     char x;
     for(int i=1;i<=N;i++)
     for(int j=1;j<=M;j++){
         fin>>x;
         if(x=='.') a[i][j]=0;
         if(x=='*') a[i][j]=1;
         if(x=='D') a[i][j]=2;
         if(x=='I') {xi=i;yi=j;}
         if(x=='O') {xf=i;yf=j;}
         dragoni[i][j]=-1;
     }

     int incc,sfc;
     coada c;


     for(int i=1;i<=N;i++)
         for(int j=1;j<=M;j++)
             if(a[i][j]==2){
             incc=1;sfc=1;
             c[1].x=i;c[1].y=j;
             dragoni[i][j]=0;
             while(incc<=sfc){
                 for(int k=0;k<=3;k++){
                     int xx,yy;
                     xx=c[incc].x+dx[k];
                     yy=c[incc].y+dy[k];
                     if(xx&&yy&&xx<=N&&yy<=M&&(dragoni[xx][yy]==-1||dragoni[xx][yy]>dragoni[c[incc].x][c[incc].y]+1)&&a[xx][yy]!=1){
                             dragoni[xx][yy]=dragoni[c[incc].x][c[incc].y]+1;
                             c[++sfc].x=xx;
                             c[sfc].y=yy;

                     }
                 }
                 ++incc;
             }

         }

 /*    for(int i=1;i<=N;i++){
         for(int j=1;j<=M;j++)
             printf("%d ",dragoni[i][j]);
         printf("\n");
     }
 */

     int ok=1,sol;
     for(sol=0;sol<=1000*1000&&ok;sol++){
         init();
         incc=sfc=1;
         c[1].x=xi;c[1].y=yi;
         while(incc<=sfc){
             int xx=c[incc].x,yy=c[incc].y;
             ++incc;
             for(int k=0;k<=3;k++){
                 int xxx=xx+dx[k],yyy=yy+dy[k];
                 if(xxx&&yyy&&xxx<=N&&yyy<=M&&dragoni[xxx][yyy]>=sol&&barbar[xxx][yyy]==0&&a[xxx][yyy]!=1){
                     c[++sfc].x=xxx;
                     c[sfc].y=yyy;
                     barbar[xxx][yyy]=barbar[xx][yy]+1;
                 }
             }

         }
         if(barbar[xf][yf]==0) ok=0;
     }


     fout<<sol-2<<"\n";

     fin.close();
     fout.close();

  }