Cod sursa(job #1101667)

Utilizator robertstrecheStreche Robert robertstreche Data 8 februarie 2014 21:14:59
Problema Castel Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.13 kb
#include <fstream>

using namespace std;

ifstream f("castel.in");
ofstream g("castel.out");

bool ok,v[30000];
short dy[5]={0,-1,0,1,0},dx[5]={0,0,1,0,-1},y[30000],x[30000];
int i,j,nr,c1,l1,n,m,k,l,c;
int a[161][161];

int main()
{
    f>>n>>m>>k;

    for (i=1;i<=n;i++)
      for (j=1;j<=m;j++)
        {
            f>>a[i][j];
            ++nr;
            if (nr==k)
             {
                 l1=i;
                 c1=j;
             }
        }

    v[(l1-1)*m+c1]=1;
    v[a[l1][c1]]=1;
    a[l1][c1]=0;

    y[1]=l1;
    x[1]=c1;

    nr=1;
    ok=1;

    while(ok)
     {
         ok=0;

         for (i=1;i<=nr;i++)
           for (j=1;j<=4;j++)
             {
                 l=y[i]+dy[j];
                 c=x[i]+dx[j];

                 if (a[l][c] && v[a[l][c]])
                   {
                       nr++;
                       y[nr]=l;
                       x[nr]=c;
                       a[l][c]=0;
                       v[(l-1)*m+c]=1;
                       ok=1;
                   }
             }
     }

      g<<nr;

    f.close();
    g.close();
}