Cod sursa(job #55940)

Utilizator mithyPopovici Adrian mithy Data 28 aprilie 2007 17:38:59
Problema Castel Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.18 kb
#include <stdio.h>
#define NMax 100
#define MAXchei 100
#define Dir 4
#define Zid -1

int n, m, k;
int a[NMax][NMax];
int matChei[NMax][NMax];
int chei[MAXchei];
int cheiLuate = 1;

struct coada
{
   int x;
   int y;
}C[1000], aux;
int dx[] = {-1, 0, 1, 0};
int dy[] = { 0, 1, 0,-1};

int in, sf;

int xs, ys;


void read();
void make();
int testCheie(int);
void addCheie(int, int);
void write();

int main()
{
   read();
   make();
   write();
   return 0;
}
void read()
{
   int i, j, cam=1;

   FILE *fin;
   fin = fopen("castel.in", "rt");

   fscanf ( fin, "%d %d %d", &n, &m, &k );

   for (i=0; i<=n+1; i++)
       a[i][0] = a[i][m+1] = Zid;

   for (i=0; i<=m+1; i++)
       a[0][i] = a[n+1][i] = Zid;

   for (i=1; i<=n; i++)
   {
      for (j=1; j<=m; j++)
      {
         fscanf ( fin, "%d", &a[i][j] );

         if ( cam == k )
         {
            xs = i;
            ys = i;
            chei[0] = cam;
            matChei[i][j] = cheiLuate;
         }
         cam++;
      }
   }

}
void make()
{
   int i, j;

   int dirX, dirY;

   C[0].x = xs+1;
   C[0].y = ys+1;

   while ( in <= sf )
   {
      aux  = C[in++];
      

      for (i=0; i<Dir; i++)
      {
         dirX = aux.x + dx[i];
         dirY = aux.y + dy[i];

         if ( a[dirX][dirY] != Zid &&
              matChei[dirX][dirY] != cheiLuate )
         {
            if ( testCheie(a[dirX][dirY]) != -1 )
            {
               aux.x   = dirX;
               aux.y   = dirY;
               addCheie( dirX, dirY );
               matChei[dirX][dirY] = cheiLuate;
               C[sf++] = aux;
            }
         }
      }
   }
}
int testCheie(int ch)
{
   int i;
   for (i=0; i<cheiLuate; i++)
   {
      if ( chei[i] == ch )
      {
         return i;
      }
   }
   return -1;
}
void addCheie(int maxX, int maxY)
{
   int i,ch=1 ;

   ch = m * (maxX-1) + maxY;

   for (i=0; i<cheiLuate; i++)
   {
      if ( chei[i] == ch )
         return;
   }

   chei[i] = ch;
   cheiLuate++;
}
void write()
{
   FILE *fout;
   fout = fopen("castel.out", "wt");
   fprintf ( fout, "%d\n", cheiLuate-1) ;
}