Cod sursa(job #456910)

Utilizator Anamaria20Cotirlea Anamaria Anamaria20 Data 17 mai 2010 09:22:49
Problema Castel Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.22 kb
#include <stdio.h>

FILE *f,*s;

int ll[4]={0,0,1,-1}; 
int cc[4]={1,-1,0,0}; 

int i,j,k,l,m,n,x1,y1,in,sf,a,b,c,w,rez,v1[200][200],v2[200][200],v3[200][200],v4[40000];

struct vector
{
	int x;
	int y;
};

vector v5[40000];

int main()
{
	f=fopen("castel.in","r");
	s=fopen("castel.out","w");
	
	fscanf(f,"%d %d %d",&m,&n,&k);
	
	for(i=1;i<=m;i++)
	{	
		for(j=1;j<=n;j++)
		{	
			fscanf(f,"%d",&v1[i][j]);
			
			l++;
			
			v2[i][j]=l;
			
			if(k==l)
			{
				x1=i;
				y1=j;
			}	
		}	
	}
	
	v4[k]=1; 
	v3[x1][y1]=1; 
    
	in=1; 
	sf=1; 
	rez=1;
    
	v5[1].x=x1; 
	v5[1].y=y1; 
    
	w=1; 
    
	while (w!=0) 
	{ 
		in=1; 
        
		w=0; 
        
		while (in<=sf) 
		{         
			for (l=0;l<=3;l++) 
			{ 
				a=v5[in].x+ll[l]; 
				b=v5[in].y+cc[l]; 
                
				if (v3[a][b]==0&&v4[v1[a][b]]==1&&a>=1&&a<=n&&b>=1&&b<=m) 
				{ 
					sf++; 
                  
					v5[sf].x=a; 
					v5[sf].y=b; 
                    
					v4[v2[a][b]]=1; 
                    
					v3[a][b]=1; 
                    
					rez++; 
                    
					w=1; 
				}	 
			}
			
			in++; 
		} 
	} 
    
	fprintf(s,"%d",rez); 
    
	fclose(s);
	
	return 0;
}