Pagini recente » Cod sursa (job #2837993) | Cod sursa (job #2502871) | Cod sursa (job #1304972) | Cod sursa (job #2143171) | Cod sursa (job #1641930)
#include<cstdio>
using namespace std;
struct eu{int x,y;};
eu v[225001];
int i,j,n,m,a[151][151],k,b[151][151],nr=0,x,y;
bool chei[225001],c[151][151];
int linie[]={1,0,-1,0};
int coloana[]={0,-1,0,1};
int main ()
{
freopen("castel.in","r",stdin);
freopen("castel.out","w",stdout);
scanf("%d%d%d",&n,&m,&k);
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
{
nr++;
b[i][j]=nr;
scanf("%d",&a[i][j]);
if(nr==k)
{
x=i;
y=j;
}
}
chei[k]=1;
int pp=0;
int in,sf;
in=1;
sf=1;
c[x][y]=1;
v[1].x=x;
v[1].y=y;
chei[k]=1;
while(pp==0)
{
while(in<=sf)
{
int lc,cc,co;
for(co=0;co<4;co++)
{
lc=v[in].x+linie[co];
cc=v[in].y+coloana[co];
if(lc>=1&&lc<=n&&cc>=1&&cc<=n&&c[lc][cc]==0&&chei[a[lc][cc]]==1)
{
sf++;
chei[b[lc][cc]]=1;
c[lc][cc]=1;
v[sf].x=lc;
v[sf].y=cc;
}
}
in++;
}
pp=1;
in=1;
sf=0;
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
if(c[i][j]==1)
{
int co,lc,cc;
for(co=0;co<4;co++)
{
lc=i+linie[co];
cc=j+coloana[co];
if(lc>=1&&lc<=n&&cc>=1&&cc<=n&&chei[a[lc][cc]]==1&&c[lc][cc]==0)
{
pp=0;
sf++;
v[sf].x=lc;
v[sf].y=cc;
chei[b[lc][cc]]=1;
c[lc][cc]=1;
}
}
}
}
int nr1=0;
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
if(c[i][j]==1)
nr1++;
printf("%d",nr1);
return 0;
}