Cod sursa(job #1118609)

Utilizator DemnokStefan Demnok Data 24 februarie 2014 12:11:29
Problema Castel Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.14 kb
//
//  main.cpp
//  Castel
//
//  Created by Stolniceanu Stefan on 24/02/14.
//  Copyright (c) 2014 Stolniceanu Stefan. All rights reserved.
//

#include <cstdio>
int x[1000],y[1000],x1,y1,a[151][151],ok,nr,viz[22501],i,j,n,m,xp,yp,k;
const int dx[]={-1,0,1,0};
const int dy[]={0,1,0,-1};
int main ()
{
    freopen("castel.in","r",stdin);
    freopen("castel.out","w",stdout);
    scanf("%ld%ld%ld",&n,&m,&k);
    for(i=1;i<=n;i++)
        for(j=1;j<=m;j++)
        {
            nr++;
            if(nr==k)
                xp=i,yp=j;
            scanf("%ld",&a[i][j]);
        }
    viz[(xp-1)*n+yp]=viz[a[xp][yp]]=1;
    a[xp][yp]=0;
    nr=1;
    ok=1;
    x[1]=xp;
    y[1]=yp;
    while (ok)
    {
        ok = 0;
        for(i=1;i<=nr;i++)
            for(j=0;j<4;j++)
            {
                x1 = x[i] + dx[j];
                y1 = y[i] + dy[j];
                if(a[x1][y1] && viz[a[x1][y1]])
                {
                    nr++;
                    x[nr]=x1;
                    y[nr]=y1;
                    a[x1][y1]=0;
                    viz[(x1-1)*m+y1]=1;
                    ok=1;
                }
            }
    }
    printf("%ld",nr);
}