Cod sursa(job #175082)

Utilizator me_andyAvramescu Andrei me_andy Data 9 aprilie 2008 16:13:57
Problema Castel Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.92 kb
#include<fstream.h>

 ifstream f("castel.in");
 ofstream g("castel.out");
 int b[151][151],xnou,ynou,nm,x0,y0,a[151][151],dx[]={-1,1,0,0},dy[]={0,0,-1,1},n,m,i,j,ok,contor,cont[151][151],nm1;
 char cheie[70000];
struct punct
{ int x,y;
};
 punct c[70000];

int main()
{
 f>>n;
 f>>m;
 f>>ok;
 contor=1;
 for(i=1;i<=n;i++)
  for(j=1;j<=m;j++)
 {
  f>>a[i][j];
  cont[i][j]=contor;
  contor++;
  if(cont[i][j]==ok)
  {
   x0=i;
   y0=j;
  }
 }
 contor=1;
 i=1;
 nm=1;
 c[1].x=x0;
 c[1].y=y0;
 cheie[cont[x0][y0]]=1;
 while(i<=nm)
 { nm1=nm;
   for(i=1;i<=nm1;i++)
   {
  for(j=0;j<=3;j++)
  {
   xnou=c[i].x+dx[j];
   ynou=c[i].y+dy[j];
   if(b[xnou][ynou]==0 && cheie[a[xnou][ynou]]==1)
    {
     b[xnou][ynou]=1;
     contor++;
     nm++;
     c[nm].x=xnou;
     c[nm].y=ynou;
     cheie[cont[xnou][ynou]]=1;
    }
  } }
  }
 g<<(contor-1);
 f.close();
 g.close();
 return 0;
}