Cod sursa(job #56458)

Utilizator alextheroTandrau Alexandru alexthero Data 29 aprilie 2007 16:50:28
Problema Castel Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.95 kb
#include <stdio.h>

#define nmax 155

int vx[] = {1,-1,0,0};
int vy[] = {0,0,1,-1};
int n,m,i,j,p,crt,sx,sy,camera[nmax][nmax],v[nmax][nmax],trebe[nmax][nmax],avem[nmax * nmax];

inline int bun(int x,int y) {
	if(x < 1 || y < 1 || x > n || y > m) return 0;
	if(!avem[trebe[x][y]]) return 0;
	if(v[x][y] > 16) return 0;
	return 1;
}

void parc(int x,int y) {
	avem[camera[x][y]] = 1;
	v[x][y] ++;
	for(int i = 0; i < 4; i++) 
		if(bun(x + vx[i],y + vy[i])) parc(x + vx[i],y + vy[i]);
	for(int i = 0; i < 4; i++) 
		if(bun(x + vx[i],y + vy[i])) parc(x + vx[i],y + vy[i]);
}

int main() {
	freopen("castel.in","r",stdin);
	freopen("castel.out","w",stdout);

	scanf("%d%d%d",&n,&m,&p);
	crt = 0;

	for(i = 1; i <= n; i++) 
		for(j = 1; j <= m; j++) {
			scanf("%d",&trebe[i][j]);
			camera[i][j] = ++crt;
			if(crt == p) sx = i,sy = j;
		}

	parc(sx,sy);
	int tot = 0;
	for(i = 1; i <= n; i++) 
		for(j = 1; j <= m; j++)
			tot += v[i][j] > 0;

	printf("%d\n",tot);

	return 0;
}