Pagini recente » Cod sursa (job #2949701) | Cod sursa (job #1883488) | Cod sursa (job #2478406) | Cod sursa (job #2339132) | Cod sursa (job #150517)
Cod sursa(job #150517)
#include<stdio.h>
FILE*fin=fopen("castel.in","r");
FILE*fout=fopen("castel.out","w");
#define maxn 151
struct nod
{
int inf;
nod*urm;
};
int a[maxn][maxn],cheie[maxn*maxn],c[maxn*maxn];
int main()
{
int i,j,col[4],lin[4],m,n,k,l,x,y,st,dr,cam;
nod*acc[maxn],*q,*z;
col[0]=0;col[1]=1;col[2]=0;col[3]=-1;
lin[0]=-1;lin[1]=0;lin[2]=1;lin[3]=0;
fscanf(fin,"%d%d%d",&n,&m,&k);
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
fscanf(fin,"%d",&a[i][j]);
fclose(fin);
for(i=1;i<=n*m;i++)
{
acc[i]=NULL;
cheie[i]=0;
}
st=dr=1;
c[st]=k;
while(st<=dr)
{
cheie[c[st]]=1;
if(c[st]%n==0)
{
i=c[st]/n;
j=m;
}
else
{
i=c[st]/n+1;
j=c[st]%n;
}
for(l=0;l<=3;l++)
{
x=i+lin[l];
y=j+col[l];
if(1<=x&&x<=n&&1<=y&&y<=m)
{
cam=m*(x-1)+y;
if(!cheie[cam])
if(!cheie[a[x][y]])
{
q=new nod;
q->inf=cam;
q->urm=acc[a[x][y]];
acc[a[x][y]]=q;
}
else
{
dr++;
c[dr]=cam;
}
}
}
q=acc[c[st]];
while(q)
{
dr++;
c[dr]=q->inf;
z=q;
q=q->urm;
delete z;
}
delete q;
acc[c[st]]=NULL;
st++;
}
fprintf(fout,"%d",dr);
fclose(fout);
return 0;
}